Withevents VS Handles

michaelrawi

Newcomer
Joined
Jul 24, 2006
Messages
15
Location
Surabaya, East Java, Indonesia
Supposed I want to create a button, and only need it's click event. Which one should I use:

Code:
dim withevents button1 as windows.forms.button

or

Code:
dim button1 as windows.forms.button
addhandler button1.click,addresof button1_click

Which is the best and why?

Thanks in advance
 
To make your VB code pretty, use the
Visual Basic:
 tags, and to make C# code look pretty use [CS] tags.

As for the difference between WithEvents and AddHandler, the only difference is that WithEvents does the wiring automatically. That means that if you declare a WithEvents variable, every time you assign an object to the variable, the handlers are automatically attatched and every time you remove an object from a variable (assign another object or assign Nothing to the variable) the handlers are removed. 

AddHandler/RemoveHandler gives you more control over how event handlers at attatched and removed, but if WithEvents suits your needs then that is probably your best bet because it requires a smaller amount of clearer code (but is harder to port to other languages).

Both methods basically compile down to the same thing.
 
Back
Top