Share via


Adding event handler to ToolStripMenuItem

Question

Thursday, May 22, 2014 3:31 PM

I have a toolstripmenu control that I added to my application. I added a new dropdown called, "Ticket Form". Dynamically, I am adding the menu items to this dropdown. I haven't found a way to add a click event to these items. When I look up examples, they are adding an eventhandler with event "Click", but it doesn't show up in intellisense.

 [ File | Edit | Ticket Form | Help ] - added via the designer
                           |
                           |- <dynamically added - need event handler>
                           |
                           |

    Private Sub LoadDropdownTicketForms()

        Dim ddname As String = "TktForm"

        Try
            For x As Integer = 1 To paramsList.Count
                Dim ts As New ToolStripMenuItem
                ts.Name = ddname & x.ToString
                ts.Text = paramItem(eCrumsTicketParam.Description, x)

                'Need Event Handler!!!!!

                ToolStripMenuItem1.DropDown.Items.Add(ts)
            Next
        Catch ex As Exception

        End Try

    End Sub

Thank you for your time!

Thanks! ~Gone2TheDogs

All replies (6)

Thursday, May 22, 2014 4:03 PM ✅Answered | 2 votes

Hello,

The following shows a conceptual example using a Button but holds true for any other control to add an event. The first shows a method which is meant to be permanent (hard to remove) while the other shows a method that is easy to remove if needed. Either method once in use you need to see who called the event, in the case below we might use the name of the control.

So for you simply adapt to ToolStripMenuItem.

Dim Sample1 As New Button With {.Top = 1, .Left = 1, .Name = "X1", .Text = "Button1"}
Dim Sample2 As New Button With {.Top = 40, .Left = 1, .Name = "X2", .Text = "Button2"}
Dim Sample3 As New Button With {.Top = 80, .Left = 1, .Name = "X3", .Text = "Button3"}

AddHandler Sample1.Click, Sub(sender As Object, e As EventArgs)
                              MessageBox.Show(CType(sender, Button).Name)
                          End Sub

AddHandler Sample2.Click, AddressOf Shared_Click
AddHandler Sample3.Click, AddressOf Shared_Click

Me.Controls.AddRange(New Control() {Sample1, Sample2, Sample3})

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Thursday, May 22, 2014 4:07 PM ✅Answered | 1 vote

Hi,

 You can use the AddHandler statement to add the the same event handler sub to all your menu items and then in the event handler sub you can cast the sender object into a ToolStripMenuItem. Then you can access all the properties of the menu item that was clicked such as the Name, Text, or the Tag properties and use that info to identify which one was clicked using the Select Case.

 I used a Select Case for the Name of the menuitems but, you may not know how many items are going to be added to the dropdown so you may want to set it up differently somehow.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDropdownTicketForms()
    End Sub

    Private Sub LoadDropdownTicketForms()

        Dim ddname As String = "TktForm"

        Try
            For x As Integer = 1 To paramsList.Count
                Dim ts As New ToolStripMenuItem
                ts.Name = ddname & x.ToString
                ts.Text = paramItem(eCrumsTicketParam.Description, x)
                AddHandler ts.Click, AddressOf TS_Clicked
                ToolStripMenuItem1.DropDown.Items.Add(ts)
            Next
        Catch ex As Exception

        End Try

    End Sub

    Private Sub TS_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim MenuItem As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
        Select Case MenuItem.Name
            Case "TktForm1"
                MessageBox.Show("TktForm1 clicked")
            Case "TktForm2"
                MessageBox.Show("TktForm2 clicked")
            Case "TktForm3"
                MessageBox.Show("TktForm3 clicked")

                'Add however many case statements you need
        End Select
    End Sub
End Class

Thursday, May 22, 2014 5:02 PM

Wow! 2 great replies, but I must give mark one of these as the answer. Since this one is more direct, I'll use it.

Thanks IronRazerz!

Thanks! ~Gone2TheDogs


Thursday, May 22, 2014 5:04 PM

I can tell you are an instructor! :-)

Your reply does very well in showing me how the handler works with controls. Thank-you Kevin!

Thanks! ~Gone2TheDogs


Thursday, May 22, 2014 5:05 PM

Wow! 2 great replies, but I must give mark one of these as the answer. Since this one is more direct, I'll use it.

Thanks IronRazerz!

Thanks! ~Gone2TheDogs

 Your Welcome.  :)

 You can mark as many as you want as answers if you find they work.


Thursday, May 22, 2014 5:20 PM | 1 vote

FWIW:

Also keep in mind that for the ToolStripItem in particular, there is a constructor overload which takes a delegate to the click event handler so you can supply a lambda method at the time you create the item:

ToolStrip1.Items.Add(New ToolStripButton("Test", Nothing, Sub(s As Object, ee As EventArgs)
                                                              Dim btn As ToolStripButton = CType(s, ToolStripButton)
                                                              MessageBox.Show("Clicked the " & btn.Text & " button.")
                                                          End Sub))

This is often the easiest way to write dynamic event handlers for tool strip items because any other code related to the method creating the item is still in scope and accessible to the lambda.

Reed Kimble - "When you do things right, people won't be sure you've done anything at all"