Context Menu Event Handling

byoung

Newcomer
Joined
Jan 23, 2003
Messages
9
Hello,

I have Context Menus that are dynamically built. I want to be able to dynamically create events for these menu choices.

For example if the menu items contain 3 choices, I want to create three events for those choices dynamically. Or is there a way to just poll the result of that menu and then call one event?

Any ideas how to do this??

Barry
 
I believe you want to use AddHandler to "hook" a click event to each menu. I'm not familiar with VB but I'm sure someone has some code they could post if this didn't help :)

-nerseus
 
Add Handler..

Hello,

I guess my question is how do you create an event dynamically?

Once I can do this, I know how to do the AddHandler code.

Thanks!

Barry
 
I belive in VB it's just a function that matches the event type. I'd drop on a design-time context menu, add a click event handler and then use that function's definition (sender as object, etc.) for yours. Sorry I can't help more... dern VB/C# differences :)

-ner
 
Something like:

AddHandler CxMenu.Click, New EventHandler(AddressOf CxMenu_Click)

CxMenu_Click is your function...

Sub CxMenu_Click(ByVal sender As Object, ByVal e As EventArgs)
 
Sub CxMenu1_Click(ByVal sender As Object, ByVal e As EventArgs)

Sub CxMenu2_Click(ByVal sender As Object, ByVal e As EventArgs)

Sub CxMenu3_Click(ByVal sender As Object, ByVal e As EventArgs)
.
.
.
Sub CxMenu100_Click(ByVal sender As Object, ByVal e As EventArgs)



How do you create these sub or procs dynamically?

Lets say I had MenuItem1, MenuItem2,MenuItem3.... MenuItem100

Obviously I don't want to statically code 100 Sub procedures, because I may have 101 choices. Then the code will break.

So as I am looping through creating the menu choices, I want to:

(A) Dynamically create a sub as a AddHandler for each Context Menu Item.


OR

(B) Create one Handler, get the Context Menu Selected Value, and then branch with Select Case to different code snippets within the Sub.

Did I clarify myself?

Thanks for any Ideas!

Barry
 
You create a generic handler which you wire up to every menuitem. The sender parameter of the event is the menuitem that raised the event.

Visual Basic:
Sub CxMenu1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim m As MenuItem = DirectCast(sender, MenuItem)

    'Process depending on what m is
End Sub
 
Back
Top