Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Supposed I want to create a button, and only need it's click event. Which one should I use:

 

dim withevents button1 as windows.forms.button

 

or

 

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

 

Which is the best and why?

 

Thanks in advance

  • Leaders
Posted

To make your VB code pretty, use the

 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.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...