BlakeMcKenna
asked on
Which is better WithEvents or AddHandler?
I have a timer control in a windows app and was wondering what is more accepted...declaring a variable as
or
Thanks!
Private WithEvents tmr As New Timer
or
AddHandler tmr.Tick, AddressOf tmr_Tick
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
More on WithEvents and the Handles clause:
http://msdn.microsoft.com/en-us/library/stf7ebaz%28v=vs.90%29.aspx
-saige-
http://msdn.microsoft.com/en-us/library/stf7ebaz%28v=vs.90%29.aspx
-saige-
WithEvents is a little easier to use, but AddHandler is often better because it enables you to do a few things that WithEvents cannot do. The same goes for the Handles clause that Visual Studio automatically adds when you create an event procedure in the "usual" way. The Handles can be deleted and replaced by AddHandler, to be able to profit from the following.
You can decide at which point you activate the event. I have often seen programmers having performance problems when loading or refreshing a DataGridView. This is because the validation events are called on each cell, even if you do it with one line of code by setting the Datasource property. If you have tight validations in the grid, this can be a lot of code to run. Most of the time, this is useless, because you are feeding the grid with data that comes from a source where it has already been validated, such as a database. Validation is then necessary only when the user types. Working with AddHandler, you can delay the activation of the events. The grid fills a lot faster if you wait after it is full before calling AddHandler. Same thing when you fill a ComboBox or a ListBox.
You are not limited to use AddHandler with variables defined at the class scope as is the case with WithEvents. It can be useful sometimes to handle events on a local variable, something that you cannot do with WithEvents
You can decide at which point you activate the event. I have often seen programmers having performance problems when loading or refreshing a DataGridView. This is because the validation events are called on each cell, even if you do it with one line of code by setting the Datasource property. If you have tight validations in the grid, this can be a lot of code to run. Most of the time, this is useless, because you are feeding the grid with data that comes from a source where it has already been validated, such as a database. Validation is then necessary only when the user types. Working with AddHandler, you can delay the activation of the events. The grid fills a lot faster if you wait after it is full before calling AddHandler. Same thing when you fill a ComboBox or a ListBox.
You are not limited to use AddHandler with variables defined at the class scope as is the case with WithEvents. It can be useful sometimes to handle events on a local variable, something that you cannot do with WithEvents
ASKER