Manually Adding Event Handlers in VB.NET
Typically when we write our code, the event handlers get wired up for us using the handles clause. So we never have to worry about wiring up our event handlers manually.
But what about the case where we want to dynamically add a control to our Windows Form or our ASP.NET page? For example, add a button. How would you respond to the button click event?
In CSharp, there is no handles clause, so figuring out how to manually wire up the event handler is simply a matter of inspecting the dotNet code and doing a copy/paste/modify operation in the editor.
The syntax for adding event handlers manually is not that difficult.
AddHandler m_button.Click, AddressOf buttonClickMethod
If you’ve written any threading code, you’ll notice that this looks similar to the code you might have written for that.
The AddHandler statement takes two parameters. The first is the event we are going to handle–in this case, the click event from the object that m_button is pointing to.
The second parameter is a pointer to a function that will handle the event. What is unique about this is that it can be a method that is part of the current class, which is what the code above is referencing, or it can be a method in another object, or even a method that is shared in another class.
To reference a method in another object
AddHandler m_button.Click, _ AddressOf SomeOtherObject.buttonClickMethod
AddHandler m_button.Click, _ AddressOf SomeClass.buttonClickMethod
Which gives us quite a bit of flexibility when we dynamically wire up our events.
Other post in Advanced VB.NET
- VB.NET - Char from String with Option Strict - April 8th, 2009
- .Net String Pool – Not Just For The Compiler - April 22nd, 2009
- VB.NET Hide Module Name - June 22nd, 2009
- Manually Adding Event Handlers in VB.NET - July 15th, 2009
- VB.NET Nullable Value Types - July 22nd, 2009
- VB.NET Processing Before WinForm Display - August 6th, 2009
Other Related Items:
Movie Night Red Carpet Aisle Runner (15 ft)This red aisle runner made just for movie night lets you roll out the red carpet just like your house will be home to the Oscars or a hot movie premiere! Runner is a thin red fabric. 15' long x 2' wide.
WARNING: CHOKING HAZARD - Small parts. Not for children under 3 years.
Juveker Keratin Hair Building Fibers - Color Light Blond; 28 Grams (Net Wt .98oz)The solution to instantly conceal bald spots and thicken fine hair is finally here-and it's called Juveker Hair Building Fibers. Juveker is not intend... Read More >
12 Breast Cancer Awareness Pink Ribbon Car MagnetsBreast Cancer Awareness Ribbon Car Magnets. Carry the message of breast cancer awareness while en route! Display a message of support anywhere and everywhere with these magnetic car ribbons. This set of twelve gives you enough to share with friends! 8"










Great post buddy..helped me