Sending arguments to a context menu event handler

D

Don Peters

I can't seem to find an answer to this problem in spite of many searches.

I have a VB.NET program that has a context menu popup event. In it I
associate some menu items with their events, as follows:

ContextMenu1.MenuItems.Add("+1", New SystemEventHandler(AddressOf
Me.Plus1))
ContextMenu1.MenuItems.Add("+2", New SystemEventHandler(AddressOf
Me.Plus2))
 
S

SStory

So why not just pass address the same sub to each?

Should work fine.

Am I missing something here?

Shane
 
D

Don Peters

Yes, that's what I want to do. But when that Sub is called, it needs to know
who called it, e.g., what was the associated menu item. I'm wondering if I
somehow can get this info from Sub argument e.

Don
 
C

Chris Dunaway

Yes, that's what I want to do. But when that Sub is called, it needs to know
who called it, e.g., what was the associated menu item. I'm wondering if I
somehow can get this info from Sub argument e.

Don

That's what the sender property is for. You can cast the sender property
to a menu item and then look at the menu's caption to determine which one
was selected:

Private Sub MenuHandler(ByVal sender As Object, e As System.EventArgs)
Select Case DirectCast(sender, MenuItem).Text
Case "+1"
'Do something is +1 was selected
Case "+2"
'Do something is +2 was selected
End Select
End Sub
 
S

SStory

Ditto.

Chris is right. That is what I was going to say. Sender should give it to
you.

Shane
 

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