Cody C
Cody C

Reputation: 3127

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...

Upvotes: 9

Views: 77625

Answers (12)

Nicky
Nicky

Reputation: 11

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

Upvotes: 1

Alex
Alex

Reputation: 1

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

Upvotes: 0

JM Varquez
JM Varquez

Reputation: 11

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

Upvotes: 1

Guest_Bob
Guest_Bob

Reputation: 11

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

Upvotes: 1

Franz David
Franz David

Reputation: 11

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.

Upvotes: 1

Mark
Mark

Reputation: 49

Can also do the following:

Dim TabName As String

TabName = YourTabControl.SelectedTab.Name

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

Upvotes: 0

user1841730
user1841730

Reputation: 91

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

Upvotes: 9

winshots
winshots

Reputation: 111

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

Upvotes: 11

Meta-Knight
Meta-Knight

Reputation: 17875

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

Upvotes: 2

Stephen Wrighton
Stephen Wrighton

Reputation: 37850

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 

Upvotes: 1

Graviton
Graviton

Reputation: 83306

TabControl.SelectedTab.

Here's the link.

Upvotes: 4

Austin Salonen
Austin Salonen

Reputation: 50235

TabControl.SelectedTab

Upvotes: 22

Related Questions