Results 1 to 6 of 6

Thread: Addhandler??

  1. #1

    Thread Starter
    Addicted Member wolfofthenorth's Avatar
    Join Date
    Jan 2001
    Location
    Tatooine
    Posts
    169

    Addhandler??

    When/Why would you ever want to use this option? Is there a time when you can use this that you can't use the event directly?
    That which does not kill us, only makes us stronger.

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    AddHandler exposes the event..if you dont use that to map an event to a sub routine, it wont fire..it is not like VB6 where you could stick code in:

    Sub Button_Click()

    but if you are using VS .NET, it does the AddHandler code for you I beleive when you select an event...I think, so that maybe why you where confused by AddHandler and its use...
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Addicted Member wolfofthenorth's Avatar
    Join Date
    Jan 2001
    Location
    Tatooine
    Posts
    169
    Thanks for the help.
    That which does not kill us, only makes us stronger.

  4. #4
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    AddHandler will mostly be used to link events to event handlers for controls that are created at run time.
    eg. You want to load controls based on the data in a database. Because there are no control arrays anymore, you need to add event handlers for the controls created at runtime with the AddHandler method.

  5. #5
    Tygur
    Guest
    VB.NET has two ways of mapping events to subroutines. There's AddHandler, and there's the Handles keyword. Check the help files for information on how to use them. I think it's just a matter of preference which one you use, but AddHandler is more flexible, becuase you're setting which Sub handles what event at run time. This means you can set a particular Sub to handle an event, then later unset it (with RemoveHandler), and possibly set it to a different Sub if you want. I've used both of them at one time or another. They both work fine. However, note this warning from Microsoft (by WithEvents they mean the Handles keyword):
    You can use either approach, but you should not use both WithEvents and AddHandler with the same event.

  6. #6

    Thread Starter
    Addicted Member wolfofthenorth's Avatar
    Join Date
    Jan 2001
    Location
    Tatooine
    Posts
    169
    Thanks for the help guys. I appreaciate your efforts.

    I have done some reading and found out some additional information... if anyone is interested.

    For the most part it does seem to be a matter of preference on which way to go. But it can also effect object lifetime. The event handler will hold a reference to the client, and unless you use RemoveHandler, the objects will remain in memory until the object with the eventsink is freed.
    That which does not kill us, only makes us stronger.

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