|
-
Mar 10th, 2003, 12:30 AM
#1
Thread Starter
Lively Member
[Resolved] Creating event handlers at runtime
I am writing a C# app that creates menu items dynamically. I need to create some events for these menus at runtime. Each one of these menu items can be run through the same function, I just need to hook up a click event at runtime. (i.e. they can all run through the same basic event handler and I can create the appropriate function to handle them all.) I just need to know how to create a single event handler for all of them or create individual click events for each menu item. if you can help... thanks in advance.
Last edited by Harvester; Mar 10th, 2003 at 05:34 PM.
-
Mar 10th, 2003, 12:38 AM
#2
Frenzied Member
Try this
Code:
mnuFile.Click += new System.EventHandler(NameOfFunctionToHandleEvent);
To unsubcribe to that event you would do this
Code:
mnuFile.Click -= new System.EventHandler(NameOfFunctionToHandleEvent);
Hope that helped
Dont gain the world and lose your soul
-
Mar 10th, 2003, 01:56 PM
#3
Thread Starter
Lively Member
Thanks
Exactly what I needed, thanks.
One question about unsubscribing. I am going to have to kill and recreate other menu items in their place. Should I unsubscribe to the event handlers before killing these menus? If I don't will I be wasting memory or will garbage collection take care of it when the menu item gets destroyed?
-
Mar 10th, 2003, 03:54 PM
#4
PowerPoster
Being subscribed to an event isn't resource intensive, so it really doesn't matter.
The way I do it though, is when I am not going to be using an object that has event subscribed to, I will unsubscribe before I destroy the object.
-
Mar 10th, 2003, 05:34 PM
#5
Thread Starter
Lively Member
Thanks again
Alright. Its simple enough to loop through them and unsibscribe to the events. Better safe than sorry I guess. Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|