简体   繁体   中英

How to test which tab is selected in VB.net TabControl

I have a TabControl with two TabPages and I was wondering what is the best way to test which tab is currently displayed? I'm not sure why I can't figure this one out...

use that tab's "ENTER EVENT " eg.

   Private Sub TabName_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabName.Enter
        MsgBox("me the tab selected")
         'or do whattever u like
    End Sub
Private Sub TabControl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl.SelectedIndexChanged
    If TabControl.SelectedTab Is tabMyTab Then
        ' do whatever...
    End If
End Sub
TabControl.SelectedTab.

这是链接

If you use .Net 3.5, you can create a IsSelected method as an extension method if you wish:

Public Module TabControlExtensions
    <Extension()> _
    Public Function IsSelected(ByVal tabPage As TabPage) As Boolean
        Dim tabControl = CType(tabPage.Parent, TabControl)
        Return (tabControl.SelectedTab Is tabPage)
    End Function
End Module

Assuming this is a WPF application, make sure that each TabItem has an Name.

Then it's just a matter of checking.

if tabItem1.IsSelected = true then
  ' Do Something 
else if tabItem2.IsSelected = true then
  ' Do Something 
end if 

Try This..

this is how to modify each of the tab when selected then there will be a function of each tab

First Grading |Second Grading |

Private Sub TabControlAction(ByVal sender As Object, ByVal e As System.EventArgs) Handles nameoftab.Click

        If nameoftab.SelectedTab.Text = "Second Grading" Then
            Msgbox("Second Grading is Selected")
''Place whatever your want

        Else
            Msgbox("First Grading is Selected")
''Place whatever your want
        End If

    End Sub

You can use if elseif else statement though.

this find works for me.

Try setting the "TAG" propety for each individual tab using the TabPages collection editor. Set each tag to a number representing the Tab sequence (starting at 1 or 0 or whatever to suit)

Private Sub TabControl1_Click(sender As Object, e As System.EventArgs) Handles TabControl1.Click

    Dim ActiveTabNumber as Integer = TabControl1.SelectedTab.Tag

End Sub
TabControl1_Click:
    If TabControl1.SelectedIndex = 0 Then
        ' Do Something       
    ElseIf TabControl1.SelectedIndex = 1 Then
        ' Do Something 
    End If
End Sub

I have a TabControl called tcMode, with members/items called tcmRelease and tcmSwitch, and the following works nicely for me with the ability to move the tabs around/rename without worrying;

If tcMode.SelectedTab Is tcmRelease Then
      'Do Something if first tab selected
ElseIf tcMode.SelectedTab Is tcmSwitch Then
      'Do something if second tab selected
End If

Image of Tab Control Members

Can also do the following:

Dim TabName As String

TabName = YourTabControl.SelectedTab.Name

If TabName.Contains("YourTabName") Then
    ' Do something
End If

This code will show the current selected tab name

 Private Sub Tab_new1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Tab_new1.SelectedIndexChanged
        MsgBox(Tab_new1.SelectedTab.Name)
    End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM