Question Tab Pages and child controls

dpatfield66

Well-known member
Joined
Apr 6, 2006
Messages
136
Programming Experience
5-10
If I have a control on a particular tab page, how can I get the index of the tab page.

Example:

I have a tab control with 3 tab pages

tabpage1 (Index = 0)
tabpage2 (Index = 1)
tabpage3 (Index = 2)

Not to be confused with the actual Tab Index of each page (which could be 22, 16, 4, or any other number)

If I have a text box on tab page 1, how can I get the program to return the Index of 0?

Or worse yet, if I have a text box inside a groupbox that's on a tab page, how can I still get the index of the tab page, by just knowing the control?
 
Check the Parent of the control until you find a TabPage.
VB.NET:
Private Sub Buttons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click, Button3.Click
    Dim p As Control = CType(sender, Control).Parent
    Do Until TypeOf p Is TabPage
        p = p.Parent
    Loop
    Me.Text = Me.TabControl1.TabPages.IndexOf(CType(p, TabPage)).ToString
End Sub
 
Back
Top