3

I create Context menu at runtime depends of text in selected cell of datagridview.
Like this:

        With ContextMenuStrip1
            .Items.Clear()
            Dim Str As String = DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value

            Dim strArr() As String = Str.Split(" ")
            For count As Integer = 0 To strArr.Length - 1
                If strArr(count).Length > 1 Then
                    .Items.Add(strArr(count))
                End If
            Next

            .Items.Add("-")
            .Items.Add("Common operation ...")
            .Items.Add("Second common operation ...")
            AddHandler .Click, AddressOf cMenu_Click

            .Show(New Point(Cursor.Position.X, Cursor.Position.Y))
        End With
etc...

Then I add event handler like this:

Private Sub cMenu_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim mytext As String
    mytext = (CType(sender, ContextMenuStrip).Text)
    Debug.Print(mytext)

    'after all...
    RemoveHandler ContextMenuStrip1.Click, AddressOf cMenu_Click
End Sub

And as vbnet beginner with this code I can't get text of fired menu item in event handler.
So please help to get it.

Wine Too
  • 4,515
  • 22
  • 83
  • 137

2 Answers2

3

Each menu item needs the handler.

Try it this way (updated with adding a shortcut key):

For count As Integer = 0 To strArr.Length - 1
  If strArr(count).Length > 1 Then
    Dim newMenu As New ToolStripMenuItem(strArr(count), _
                                         Nothing, AddressOf cMenu_Click)
    newMenu.ShortcutKeys = Keys.Control Or Keys.C
    .Items.Add(newMenu)
  End If
Next

Your click method should be changed to handle a ToolStripMenuItem instead:

Private Sub cMenu_Click(ByVal sender As Object, ByVal e As EventArgs)
  Dim mytext As String
  mytext = DirectCast(sender, ToolStripMenuItem).Text
  Debug.Print(mytext)
End Sub
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Thank you Lars! This works excellent! Should I remove handler when it is not needed anymore or I shouldn't? And if I should how to do that in this situation? – Wine Too Jun 22 '12 at 22:19
  • @user973238 I don't think it's necessary since the menu items being cleared don't exist anywhere else. There is no reference anymore. See this answer [In vb.net, if I use AddHandler, Do I have to use RemoveHandler?](http://stackoverflow.com/a/7436068/719186) – LarsTech Jun 22 '12 at 22:23
  • If you see this... it would be interesting if is possible to add a shortcut on this way created menu item. I try with: ".Items.Add("Copy whole row ..." & vbTab & "Ctrl+C", Nothing, AddressOf cMenu_Click)", but of course don't work. Decided answer that this is impossible would also mean something. – Wine Too Jun 23 '12 at 10:39
  • @user973238 Updated example. It's missing the logic for what the shortcut should be for every item in the array. The current example assigns Ctrl-C to every menu item. – LarsTech Jun 23 '12 at 12:32
  • Of course, this is not logical. But at the end of context menu we often have some constant operations like "Copy whole row". I do changes as you recommend: Dim newMenu As New ToolStripMenuItem("Copy whole row ...", Nothing, AddressOf cMenu_Click) newMenu.ShortcutKeys = Keys.Control Or Keys.C .Items.Add(newMenu) with this I get shorcut on my menu but keys Ctrl+C (or any other) don't fire event. I do obviously something wrong? – Wine Too Jun 23 '12 at 17:45
  • @user973238 Well, yeah, that won't work. Your context menu doesn't exist until the user clicks on your datagrid cell. Consider creating a menu strip (not a context strip) with those short cuts that is there when the program starts. – LarsTech Jun 23 '12 at 21:14
0

Add a handler (pointing to the same method) for the Click Event of all of the child Items of your ContextMenuStrip. Then in your method cast it as a ToolStripMenuItem or MenuItem class (whatever you're using) to find the text of the clicked item.

NoAlias
  • 9,218
  • 2
  • 27
  • 46