如何确定哪个TabPage当前处于活动状态?

Ros*_*nly 0 .net tabcontrol tabpage visual-studio winforms

我想知道窗口中的哪个标签页是活动的.我的目标是这样的:

  if Tabpage1 is active then
   .....
  end if

  if Tabpage2 is active then
  ...
  end if
Run Code Online (Sandbox Code Playgroud)

我将在表格结束时写下来.

Cod*_*ray 8

您必须查询容器控件(TabControl控件)以获取此信息,而不是TabPage它所托管的各个控件.

您有两种选择,具体取决于您希望接收的信息类型.您可以使用该SelectedIndex属性返回当前所选标签页的从零开始的索引,也可以使用该SelectedTab属性,该属性返回TabPage表示当前所选标签页的类的实例.

代码示例:

If myTabControl.SelectedIndex = 1 Then
    ' Do something for the first tab page
ElseIf myTabControl.SelectedIndex = 2 Then
    ' Do something for the second tab page
Else
    ' Uh-oh! One of the other tab pages that you didn't
    ' mention in your question is selected!
End If
Run Code Online (Sandbox Code Playgroud)

要么:

If myTabControl.SelectedTab = myFirstTabPage Then
    ' Do something for the first tab page
ElseIf myTabControl.SelectedTab = mySecondTabPage Then
    ' Do something for the second tab page
Else
    ' Uh-oh! One of the other tab pages that you didn't
    ' mention in your question is selected!
End If
Run Code Online (Sandbox Code Playgroud)