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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1472083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:22:22  来源:igfitidea点击:

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

.netvb.nettabcontrol

提问by Cody C

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

我有一个带有两个 TabPage 的 TabControl,我想知道测试当前显示哪个选项卡的最佳方法是什么?我不知道为什么我无法弄清楚这一点......

回答by user1841730

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

回答by winshots

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

回答by Graviton

TabControl.SelectedTab.

Here's the link.

这是链接

回答by Meta-Knight

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

回答by Stephen Wrighton

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 

回答by Franz David

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.

这个发现对我有用。

回答by Guest_Bob

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)

尝试使用 TabPages 集合编辑器为每个单独的选项卡设置“TAG”属性。将每个标签设置为代表 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

回答by JM Varquez

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

回答by Nicky

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

选项卡控件成员的图像