Results 1 to 5 of 5

Thread: [Resolved] Creating event handlers at runtime

  1. #1

    Thread Starter
    Lively Member Harvester's Avatar
    Join Date
    May 2002
    Location
    God's Country
    Posts
    124

    [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.

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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

  3. #3

    Thread Starter
    Lively Member Harvester's Avatar
    Join Date
    May 2002
    Location
    God's Country
    Posts
    124

    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?

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  5. #5

    Thread Starter
    Lively Member Harvester's Avatar
    Join Date
    May 2002
    Location
    God's Country
    Posts
    124

    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
  •  



Click Here to Expand Forum to Full Width