G
Guest
I know this is pretty simple but as a newbie, I struggled with determining
how to tell what item was selected by the user in a context menu. Here's the
code that I got to work to pop up a context menu on the user right mouse
click and determine which menuitem was clicked by the user. The OnMouseRight
sub below is called from the MouseUp Event when the button value of the
MouseEventArgs is Right:
Note that the form must have a context menu control named "PopMenu".
Private Sub OnMouseRight(ByVal e As System.Windows.Forms.MouseEventArgs)
Dim pos As Point = New Point(10, 10)
Dim pclick As EventHandler = New EventHandler(AddressOf handles_Pop)
PopMenu.MenuItems.Clear()
PopMenu.MenuItems.Add("Hide Column", pclick)
PopMenu.MenuItems.Add("UnHide Column", pclick)
PopMenu.MenuItems.Add("-") 'Add Separator Bar
PopMenu.MenuItems.Add("Add Column", pclick)
PopMenu.MenuItems.Add("Delete Column", pclick)
PopMenu.MenuItems.Add("-")
PopMenu.Show(Me, pos)
End Sub
Private Sub handles_Pop(ByVal sender As Object, ByVal e As EventArgs)
Dim m As MenuItem = CType(sender, MenuItem)
Dim a As String = m.Text
'Parent.GetContextMenu().SourceControl.ToString
Dim itemidx as integer = m.Index
'Note this will give you the name of the menu item and index of the
context
' menu item the user clicked
End Sub
how to tell what item was selected by the user in a context menu. Here's the
code that I got to work to pop up a context menu on the user right mouse
click and determine which menuitem was clicked by the user. The OnMouseRight
sub below is called from the MouseUp Event when the button value of the
MouseEventArgs is Right:
Note that the form must have a context menu control named "PopMenu".
Private Sub OnMouseRight(ByVal e As System.Windows.Forms.MouseEventArgs)
Dim pos As Point = New Point(10, 10)
Dim pclick As EventHandler = New EventHandler(AddressOf handles_Pop)
PopMenu.MenuItems.Clear()
PopMenu.MenuItems.Add("Hide Column", pclick)
PopMenu.MenuItems.Add("UnHide Column", pclick)
PopMenu.MenuItems.Add("-") 'Add Separator Bar
PopMenu.MenuItems.Add("Add Column", pclick)
PopMenu.MenuItems.Add("Delete Column", pclick)
PopMenu.MenuItems.Add("-")
PopMenu.Show(Me, pos)
End Sub
Private Sub handles_Pop(ByVal sender As Object, ByVal e As EventArgs)
Dim m As MenuItem = CType(sender, MenuItem)
Dim a As String = m.Text
'Parent.GetContextMenu().SourceControl.ToString
Dim itemidx as integer = m.Index
'Note this will give you the name of the menu item and index of the
context
' menu item the user clicked
End Sub