10

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!

Jimmie Lin
  • 2,205
  • 2
  • 23
  • 35

2 Answers2

21

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

TabControl has 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.)

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

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 might want to additionally guard your code against cases where SelectedIndex has an invalid value, e.g. -1.)

Edit (added after comment of OP):

If SelectedIndexChanged does not work for you because you need to catch the user's action for all mouse buttons, you could use the GetTabRect method of TabControl like this:

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
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • This will work for a regular mouseclick, but what about a middle click? Middle clicking won't change the selected tab. Thanks for this though, this case is now working but I need to check which tab was middle-clicked on, also. should I open a new question? – Jimmie Lin Feb 12 '10 at 09:17
  • It is a different question, but see my expanded answer for now. – stakx - no longer contributing Feb 12 '10 at 09:23
  • Did you ever find out how to accomplish the middle click? – PedroC88 Apr 13 '11 at 22:31
  • @Pedro, read the second part of my answer. All you would need to do is change the `If` condition to: `MyTabControl.GetTabRect(…).Contains(…)` **`AndAlso e.Button = MouseButtons.Middle`**. I just tried it, works like a charm. You will know which tab was clicked by the `tabIndex` variable. You could e.g. use it as an index into the `MyTabControl.TabPages` collection. – stakx - no longer contributing Apr 15 '11 at 17:21
3

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.

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.