Usefull Tabcontrol events

Posted in: Forms and Grids
By dePoPo
Jul 3, 2009 - 12:23:00 PM

Changing the active tab on a tabcontrol from code:

Me.TabControl1.SelectedIndex=2  '  Switch to the third tab
' Do stuff
Me.TabControl1.SelectedIndex=0  ' Back to the first tab
' More sttuff



Detecting when a tab page is beeing activated:

The Selected event on the control itsself is begin triggered when a tab change is done.

    Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected

        Msgbox("Tab changed")
       
    End Sub

Detecting which tab is active (combined with the above  tabchange event)


    Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected

        msgbox(TabControl1.SelectedIndex & " is active")
           
    End Sub

It is also possible to reference tab controls by name, instead of tab number. The example below demonstrates how to hide a tab page from a tabcontrol with a name reference

' --- Hide the tab named DemoTab
TabControl1.Controls.Remove(TabControl1.TabPages("DemoTab"))

' --- And show it again
TabControl1.Controls.Add(New TabPage("DemoTab"))



Visitor Comments