I drag Tabcontrol.& add two tab pages.

I have contextmenu Strip, dat i bound to tabcontrol.I select the ContextMenuStrip & set it to ContextMenuStrip1

Now i right click on tab headers.Suppose i right click on tabpage1,contextmenu is displayed.I want that when i right click on tabpage..I want to get the index of that tab..Even if dat tab page is selected.

Ex-Mozilla.

Private Sub TabControl1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControl1.MouseClick
        Try
            MsgBox(TabControl1.SelectedIndex.ToString)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Above event gives me the index,when i select the tab.
Plz help me out.

Recommended Answers

All 2 Replies

Here is the C# code, you should be able to port this to VB fairly easy:

private void tabControl1_MouseClick(object sender, MouseEventArgs e)
    {
      int tabIndex = GetTabIndex(e.Location);
      if (tabIndex != -1)
      {
        MessageBox.Show("You clicked on tab " + tabIndex.ToString("F0"));
      }
    }
    private int GetTabIndex(Point p)
    {
      int result = -1;
      for (int i1 = 0; i1 < tabControl1.TabCount; i1++)
      {
        if (tabControl1.GetTabRect(i1).Contains(p))
          return i1;
      }
      return result;
    }

hi frnd,I do little modification & it worked.Thx.
FINAL CODE

Public Class Form1

    Private Sub TabControl1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControl1.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Right Then
            DisplayClickedTab(e.Location)
        End If
    End Sub

    Private Sub DisplayClickedTab(ByVal clickPoint As Point)
        For i As Integer = 0 To TabControl1.TabPages.Count - 1
            If TabControl1.GetTabRect(i).Contains(clickPoint) Then
                Text = TabControl1.TabPages(i).Name
            End If
        Next
    End Sub

End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.