473,226 Members | 1,519 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

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 3666
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marcel Hug | last post by:
Hello NG ! I would like to show the context menu which apears when i drop a file by fight mouse button. Could somebody help me ? Thanks
0
by: VP | last post by:
G'day folks, well i am attempting to get an understanding on how to create the menuitems in a context menu on the fly. So far I have managed to actually achieve the menu items being created for...
3
by: jcrouse | last post by:
Is it possible more multiple controls (labels) to share the code from one context menu. I have 32 labels on a form and don't want to add 32 context menu controls. Where might I find information on...
2
by: Paul E Collins | last post by:
A context menu is created in the scope of the RightClick event, but it continues to be displayed after that event handler terminates - i.e. after it has gone out of scope. The code works correctly,...
5
by: james | last post by:
Hello, I am having a little trouble creating an event handler for a context menu toolstripmenuitem. I've seen various tutorials and so on, but I keep getting a bit stuck! So far I have a second...
2
by: Lucky | last post by:
hi guys, i'm working on the context menu for the controls. the problem i'm facing right now is like this : i want to use one context menu for more then one controls lets say i want to use one...
18
by: luco | last post by:
Hi! I'm having a problem with attachEvent function. I'd like to add attachEvent dynamically to some objects so that each could execute event function with different parameter value. The question...
2
by: Ron M. Newman | last post by:
Hi, Just need a little advice. Id like to build *dynamic* context menus for tree nodes. I'm pretty versed in building context menus and attaching them to tree nodes. My question is, what...
2
by: MCM | last post by:
I'm working on a plotting control. The plotting control will have a context menu with basic commands for "scaling", "zooming", etc. Is there a way that, from the parent form, I can add more...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.