byoung Posted January 23, 2003 Posted January 23, 2003 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 Quote
*Experts* Nerseus Posted January 24, 2003 *Experts* Posted January 24, 2003 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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
byoung Posted January 24, 2003 Author Posted January 24, 2003 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 Quote
*Experts* Nerseus Posted January 24, 2003 *Experts* Posted January 24, 2003 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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Leaders Iceplug Posted January 24, 2003 Leaders Posted January 24, 2003 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) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
byoung Posted January 24, 2003 Author Posted January 24, 2003 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 Quote
*Gurus* divil Posted January 24, 2003 *Gurus* Posted January 24, 2003 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. 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 Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.