This is one of those things where you say to yourself, “there must be an easy way to do this” but all the searching in the world isn’t netting you an example. I finally found this tidbit which gave me exactly what I wanted, although it’s disappointing to me that it requires a direct windows API call.
My issue was that I need to know when someone right-clicked on a tab that is NOT the currently selected tab in a tab control.
I finally gave up and just implemented it this way, (fortunately it was for a one-off project that didn’t require a lot of scrutiny.)
<Flags()> _
Private Enum TCHITTESTFLAGS
TCHT_NOWHERE = 1
TCHT_ONITEMICON = 2
TCHT_ONITEMLABEL = 4
TCHT_ONITEM = TCHT_ONITEMICON Or TCHT_ONITEMLABEL
End Enum
Private Const TCM_HITTEST As Int32 = &H130D
<System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)> _
Private Structure TCHITTESTINFO
Public pt As Point
Public flags As TCHITTESTFLAGS
Public Sub New(ByVal x As Int32, ByVal y As Int32)
pt = New Point(x, y)
End Sub
End Structure
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Private Shared Function SendMessage(ByVal hwnd As IntPtr, ByVal msg As Int32, ByVal wParam As IntPtr, ByRef lParam As TCHITTESTINFO) As Int32
End Function
Private Sub TabControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControl1.MouseDown
If e.Button = MouseButtons.Right Then
Dim HTI As New TCHITTESTINFO(e.X, e.Y)
Dim hotTab As TabPage = TabControl1.TabPages(SendMessage(TabControl1.Handle, TCM_HITTEST, IntPtr.Zero, HTI))
TabControl1.ContextMenu = hotTab.ContextMenu
End If
End Sub
Private Sub TabControl1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControl1.MouseUp
TabControl1.ContextMenu = Nothing
End Sub
55e3892a-683f-4eab-822a-ef636817bead|0|.0