Home » VB.NET » Manually Adding Event Handlers in VB.NET

Manually Adding Event Handlers in VB.NET

office-019

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

To reference a shared method

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 VB.NET

Related Post

  • Overriding Events in VB.NETOverriding Events in VB.NET Back in the day, you use to be able to override an event in VB by using the drop down list in the code window.  Today, I had a friend who is moving from Visual Studio 2003 to Visual Studio […]
  • VB.NET Processing Before WinForm DisplayVB.NET Processing Before WinForm Display I woke up this morning to an interesting question. “Using VB.net 2008, I want my project to be a Windows Forms Application, but upon startup, I want to check a few files to see if they […]
  • .Net String Pool – Not Just For The Compiler.Net String Pool – Not Just For The Compiler On Monday, I was corrected in my assertion that creating multiple empty strings would create multiple objects.  Turns out the compiler automatically puts all of the strings that are […]
  • VB.NET – Char from String with Option StrictVB.NET – Char from String with Option Strict So here's the question: I'm using String.Split() and need to pass in a Char or a Char array as the parameter.  If I pass in a string String.Split("/") I get an error "Option Strict […]
  • VB.NET Nullable TypesVB.NET Nullable Types SQL has long had the ability to specify that a value is NULL even if it is a primitive type, but the only way you could have a NULL value in VB.NET is if you were dealing with an […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS. Does your team need additional help in any of the above? Contact Dave today.

  • Suresh

    Great post buddy..helped me