Determining which Menu Item is Clicked

E

Elliot

The code creates two menu items at runtime and I want to be know which
menu item was clicked in the Menuitems_Click procedure.
Sender.Gettype.Name returns the value "MenuItem" only.

Any help is appreciated.


Private Sub

Dim mainMenu1 As New MainMenu()

Me.Menu = mainMenu1

' Create empty menu item objects.
Dim menuItem1 As New MenuItem("&File")
Dim menuItem2 As New MenuItem("&Edit")

' Add the menu items to the main menu.
mainMenu1.MenuItems.Add(menuItem1)
mainMenu1.MenuItems.Add(menuItem2)

' Add functionality
AddHandler menuItem1.Click, AddressOf Menuitems_Click
AddHandler menuItem2.Click, AddressOf Menuitems_Click

End Sub

Private Sub Menuitems_Click(ByVal sender As Object, ByVal e As
EventArgs)

' Code to determine which item was clicked -- menuitem1 or menuitem2.

End Sub
 
C

Chris Dunaway

that's what the sender argument of the event handler is for. It tells
you which object raised the event. You could check it this way:

If sender Is menuItem1 Then
'bla bla
Else
If sender is MenuItem2 Then
End If
End If


You might also try this, but I'm not sure if it is considered a best
practice:

Select Case True
Case sender Is MenuItem1
Case sender Is MenuItem2
End Select
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top