468,512 Members | 1,346 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Sending arguments to a context menu event handler

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))
Nov 20 '05 #1
4 3422
So why not just pass address the same sub to each?

Should work fine.

Am I missing something here?

Shane

"Don Peters" <don-peters@NOSPAM!comcast.net> wrote in message
news:uL**************@TK2MSFTNGP09.phx.gbl...
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))
.
.

And then of course I have the event procedures that are referenced:

Private Sub Plus1(ByVal sender As Object, ByVal e as System.EventArgs)
'do some stuff here
End Sub

Private Sub Plus2(ByVal sender As Object, ByVal e as System.EventArgs)
'do some stuff here
End Sub

This works fine, but since my context menu contains many items, I have to
have many of these virtually identical Subs. I'd like to have only one Sub
handle all these events. So my question is this:

Is there some way my declaration of the event handler can also pass along an argument (e.g., a string) so that I can access it in the called Sub?

-Don

Nov 20 '05 #2
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

"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote in message
news:uM**************@TK2MSFTNGP11.phx.gbl...
So why not just pass address the same sub to each?

Should work fine.

Am I missing something here?

Shane

"Don Peters" <don-peters@NOSPAM!comcast.net> wrote in message
news:uL**************@TK2MSFTNGP09.phx.gbl...
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))
.
.

And then of course I have the event procedures that are referenced:

Private Sub Plus1(ByVal sender As Object, ByVal e as System.EventArgs) 'do some stuff here
End Sub

Private Sub Plus2(ByVal sender As Object, ByVal e as System.EventArgs) 'do some stuff here
End Sub

This works fine, but since my context menu contains many items, I have to have many of these virtually identical Subs. I'd like to have only one Sub handle all these events. So my question is this:

Is there some way my declaration of the event handler can also pass
along an
argument (e.g., a string) so that I can access it in the called Sub?

-Don


Nov 20 '05 #3
On Thu, 26 Feb 2004 19:42:39 -0500, Don Peters wrote:
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

"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote in message
news:uM**************@TK2MSFTNGP11.phx.gbl...
So why not just pass address the same sub to each?

Should work fine.

Am I missing something here?

Shane

"Don Peters" <don-peters@NOSPAM!comcast.net> wrote in message
news:uL**************@TK2MSFTNGP09.phx.gbl...
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))
.
.

And then of course I have the event procedures that are referenced:

Private Sub Plus1(ByVal sender As Object, ByVal e as System.EventArgs) 'do some stuff here
End Sub

Private Sub Plus2(ByVal sender As Object, ByVal e as System.EventArgs) 'do some stuff here
End Sub

This works fine, but since my context menu contains many items, I have to have many of these virtually identical Subs. I'd like to have only one Sub handle all these events. So my question is this:

Is there some way my declaration of the event handler can also pass

along
an
argument (e.g., a string) so that I can access it in the called Sub?

-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

--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #4
Ditto.

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

Shane

"Chris Dunaway" <dunawayc@_lunchmeat_sbcglobal.net> wrote in message
news:1q*****************************@40tude.net...
On Thu, 26 Feb 2004 19:42:39 -0500, Don Peters wrote:
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

"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote in message
news:uM**************@TK2MSFTNGP11.phx.gbl...
So why not just pass address the same sub to each?

Should work fine.

Am I missing something here?

Shane

"Don Peters" <don-peters@NOSPAM!comcast.net> wrote in message
news:uL**************@TK2MSFTNGP09.phx.gbl...
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))
.
.

And then of course I have the event procedures that are referenced:

Private Sub Plus1(ByVal sender As Object, ByVal e as

System.EventArgs)
'do some stuff here
End Sub

Private Sub Plus2(ByVal sender As Object, ByVal e as

System.EventArgs)
'do some stuff here
End Sub

This works fine, but since my context menu contains many items, I
have to
have many of these virtually identical Subs. I'd like to have only one

Sub
handle all these events. So my question is this:

Is there some way my declaration of the event handler can also pass

along
an
argument (e.g., a string) so that I can access it in the called Sub?

-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

--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by VP | last post: by
3 posts views Thread by jcrouse | last post: by
2 posts views Thread by Paul E Collins | last post: by
18 posts views Thread by luco | last post: by
2 posts views Thread by Ron M. Newman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.