简体   繁体   English

如何测试在VB.net TabControl中选择了哪个选项卡

[英]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?我有一个带有两个 TabPage 的 TabControl,我想知道测试当前显示哪个选项卡的最佳方法是什么? 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:如果您使用 .Net 3.5,您可以根据需要创建一个 IsSelected 方法作为扩展方法:

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.假设这是一个 WPF 应用程序,请确保每个 TabItem 都有一个名称。

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.您可以使用 if elseif else 语句。

this find works for me.这个发现对我有用。

Try setting the "TAG" propety for each individual tab using the TabPages collection editor.尝试使用 TabPages 集合编辑器为每个单独的选项卡设置“TAG”属性。 Set each tag to a number representing the Tab sequence (starting at 1 or 0 or whatever to suit)将每个标签设置为代表 Tab 序列的数字(从 1 或 0 或任何适合的数字开始)

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;我有一个名为 tcMode 的 TabControl,其成员/项目名为 tcmRelease 和 tcmSwitch,以下对我来说效果很好,能够移动标签/重命名而无需担心;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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