vb.net 在 tabcontrol MouseClick 事件上单击了哪个选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2250544/
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-09 14:41:22  来源:igfitidea点击:

Which tab was clicked on tabcontrol MouseClick event

vb.netwinformstabcontrolmouseclick-event

提问by Jimmie Lin

I am learning how to develop Windows Forms applications with Visual Basic Express 2008, and my testing/learning application has a TabControl with a few test pages (3, for example, the number isn't relevant here).

我正在学习如何使用 Visual Basic Express 2008 开发 Windows 窗体应用程序,我的测试/学习应用程序有一个 TabControl 和几个测试页面(例如,3 个,这里的数字不相关)。

Now, I am handing the MouseClick event on the Tabcontrol, and I can't seem to be able to figure out how to get which tab was clicked on. I believe that the MouseClick event isn't fired if I click on another place of the tab strip, therefore a tab must have been clicked on. The problem is, which was the tab?

现在,我在 Tabcontrol 上处理 MouseClick 事件,但我似乎无法弄清楚如何获取点击了哪个选项卡。我相信如果我单击选项卡条的另一个位置,则不会触发 MouseClick 事件,因此必须单击了一个选项卡。问题是,哪个是标签?

Any help would be appreciated. Thanks!

任何帮助,将不胜感激。谢谢!

回答by stakx - no longer contributing

Don't use the MouseClickevent, because there is another event better suited for this purpose:
(Note: edited after the OP has posted a comment.)

不要使用该MouseClick事件,因为还有另一个更适合此目的的事件:(
注意:在 OP 发表评论后编辑。)

TabControlhas a property SelectedIndex. This is the zero-based number of the currently selected tab. (There is also another property called SelectedTab, referring directly to the selected tab page object.)

TabControl有一个属性SelectedIndex。这是当前选定选项卡的从零开始的数字。(还有另一个名为 的属性SelectedTab,直接引用选定的标签页对象。)

You can hook an event handler to the event SelectedIndexChangedin order to be notified whenever the user selects another tab:

您可以将事件处理程序挂钩到该事件SelectedIndexChanged,以便在用户选择另一个选项卡时收到通知:

Private Sub MyTabControl_SelectedIndexChanged(ByVal sender As Object, _
                                              ByVal e As System.EventArgs) _
            Handles MyTabControl.SelectedIndexChanged

    Dim indexOfSelectedTab As Integer = MyTabControl.SelectedIndex
    Dim selectedTab As System.Windows.Forms.TabPage = MyTabControl.SelectedTab

    ...

End Sub

(Take note that you mightwant to additionally guard your code against cases where SelectedIndexhas an invalid value, e.g. -1.)

(请注意,您可能需要额外保护您的代码,以防SelectedIndex出现无效值的情况,例如-1。)

Edit (added after comment of OP):

编辑(在 OP 评论后添加)

If SelectedIndexChangeddoes not work for you because you need to catch the user's action for all mouse buttons, you could use the GetTabRectmethodof TabControllike this:

如果SelectedIndexChanged因为你需要捕捉用户的动作对所有的鼠标按键不适合你,你可以使用该GetTabRect方法TabControl是这样的:

Private Sub MyTabControl_MouseClick(sender As Object, _
                                    e As System.Windows.Forms.MouseEventArgs) _
            Handles MyTabControl.MouseClick

    ...

    For tabIndex As Integer = 0 To MyTabControl.TabCount - 1
        If MyTabControl.GetTabRect(tabIndex).Contains(e.Location) Then
           ...  ' clicked on tab with index tabIndex '
        End If
    Next

    ...

End Sub

回答by Thurman Jenner

The simplest way I use to handle this would be to use the ENTER and LEAVE events for each tabpage's event. For Example I have it so that when I leave the first page it changes the visible property to my datagridview to False and when I enter it turns it back to True and loads the data for my table.

我用来处理这个问题的最简单的方法是为每个标签页的事件使用 ENTER 和 LEAVE 事件。例如,我有它,所以当我离开第一页时,它将我的 datagridview 的可见属性更改为 False,当我输入它时,它将它返回到 True 并为我的表加载数据。

Private Sub TabPage1_Enter(sender As System.Object, e As System.EventArgs) Handles TabPage1.Enter
    DataGridView1.Visible = True
    Load_Table()
End Sub

Private Sub TabPage1_Leave(sender As System.Object, e As System.EventArgs) Handles TabPage1.Leave
    DataGridView1.Visible = False
End Sub

If you wanted to change the text on the control or something like that I suppose the index method would be the choice way to go, but just to know which tab was clicked that is what these two events are for.

如果您想更改控件上的文本或类似的内容,我想 index 方法将是首选方法,但只是想知道单击了哪个选项卡,这就是这两个事件的用途。