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).
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?
Any help would be appreciated. Thanks!
Don’t use theMouseClickevent, because there is another event better suited for this purpose:(Note: edited after the OP has posted a comment.)
TabControlhas a propertySelectedIndex. This is the zero-based number of the currently selected tab. (There is also another property calledSelectedTab, referring directly to the selected tab page object.)You can hook an event handler to the event
SelectedIndexChangedin order to be notified whenever the user selects another tab:(Take note that you might want to additionally guard your code against cases where
SelectedIndexhas an invalid value, e.g.-1.)Edit (added after comment of OP):
If
SelectedIndexChangeddoes not work for you because you need to catch the user’s action for all mouse buttons, you could use theGetTabRectmethod ofTabControllike this: