0

Hello Community,
I'm trying to dynamically add a menuitem to my context menu and add a click handler but for some reason it won't add the handler what did a do wrong?

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each Line In RichTextBox1.Lines
            Dim NewItem As New MenuItem
            NewItem.Text = Line
            AddHandler NewItem.Click, AddressOf NewItem_Click
            ContextMenuStrip1.Items.Add(Line)
        Next
    End Sub

    Private Sub NewItem_Click(sender As Object, e As EventArgs)
        MsgBox(CType(sender, MenuItem).Text)
    End Sub
3
Contributors
3
Replies
13
Views
4 Years
Discussion Span
Last Post by ghincelino
0

try this:

        Dim NewItem As ToolStripMenuItem = New ToolStripMenuItem
        NewItem.Text = Line
        AddHandler NewItem.Click, AddressOf NewItem_Click
        ContextMenuStrip1.Items.Add(NewItem)

Edited by tinstaafl

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.