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.

Dani AI

Generated

Nice work @sonia sardana. Building on ’s approach, the cleanest way to tie a ContextMenuStrip to the tab header you right-clicked is to resolve the tab index in the menu’s Opening event. That fires after the right-click but before the menu shows, so you can both identify the tab under the cursor and cancel the menu if the user clicked empty space. This avoids relying on SelectedIndex and works even when the clicked tab is not active.

Here is a drop-in VB.NET pattern you can use:

' Form-level field so menu commands know which tab was clicked
Private _contextTabIndex As Integer = -1

Private Sub ContextMenuStrip1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) _
    Handles ContextMenuStrip1.Opening

    ' Convert screen mouse position to TabControl client coords
    Dim pt As Point = TabControl1.PointToClient(Cursor.Position)
    _contextTabIndex = TabIndexAt(TabControl1, pt)

    ' No tab under the mouse? Do not show the menu.
    If _contextTabIndex = -1 Then
        e.Cancel = True
        Return
    End If

    ' Optional: focus the tab you right-clicked
    TabControl1.SelectedIndex = _contextTabIndex
End Sub

Private Function TabIndexAt(tc As TabControl, pt As Point) As Integer
    For i As Integer = 0 To tc.TabCount - 1
        If tc.GetTabRect(i).Contains(pt) Then Return i
    Next
    Return -1
End Function

' Example menu command using the captured index
Private Sub CloseTabToolStripMenuItem_Click(sender As Object, e As EventArgs) _
    Handles CloseTabToolStripMenuItem.Click

    If _contextTabIndex >= 0 AndAlso _contextTabIndex < TabControl1.TabPages.Count Then
        TabControl1.TabPages.RemoveAt(_contextTabIndex)
    End If
End Sub

Tips:

  • If the menu is invoked via keyboard (Shift+F10), there is no mouse location; fall back to TabControl1.SelectedIndex.
  • Cancel Opening when the click is not on a header so you do not show a misleading menu.
  • This works with multiline and scrollable tabs because GetTabRect returns header bounds in control coordinates.

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.