CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

Handles, AddHandler and RemoveHandler in VB.NET

After a crash course comes sinking in. This post is a rewrite of yesterdays one on events in VB.NET. I will concentrate on VB.NET include Daniel and Chi's comment and after that I will return to my beloved C#.

There are several ways to declare events in VB.NET. The usual one is using the handles keyword. As Daniel pointed out this can link an event handling method to multiple events.

    Private Sub MyClick(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click, Button2.Click
        ListBox1.Items.Add(String.Format("You clicked {0}", CType(sender, Control).Name))
    End Sub
 

This code will run on a click of button1 as well as a click of button2. The coupling of the events and handler is declared in the method signature.

You can also add an eventhandler dynamically form code.

    Private Sub MyOtherClick(ByVal sender As Object, ByVal e As EventArgs)
        ListBox1.Items.Add("That other click")
    End Sub

    Private Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click
        AddHandler Button2.Click, AddressOf MyOtherClick
    End Sub
 

Every time ButtonAdd is clicked it will add another event handler to Button2. When ButtonAdd is clicked 4 times a click of button2 will result in MyClick being executed first after which MyOtherClick is executed four times in a row.

You can also dynamically remove eventhandlers. This is not limited to handlers been set dynamically. This removes the event handler which was set with the handles keyword.

    Private Sub ButtonRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRemove.Click
        RemoveHandler Button2.Click, AddressOf MyClick
    End Sub
 

This is no big surprise as reflector had already shown that the Handles keyword translates to an addhandler statement.

Chid pointed out that you do not need to use the WithEvents keyword on an object when it's eventhandlers are set using AddHandler. Reflector shows that this does not make any difference at all. Just like handles, withevents is nowhere to be found in the actual generated code. In fact it all boils down to AddHandler and RemoveHandler which map to CLR functions. In this post I've shown that you can freely mix Handles and AddHandler. In last post Reflector showed that there are some subtltties behind the scenes. These subtleties are enough to make or break the sinking of COM events. I'm gratefull for VB.NET fixing that for me. And a little sad for C# not doing what I had hoped to do. After all it's eventhandling syntax is imho far less confusing.


Published Aug 03 2005, 07:34 PM by pvanooijen
Filed under:

Comments

Peter's Gekko said:

The term RAD is more or less a hot topic on Codebetter. It started with a "RAD kills" post by Jeffrey...
# April 25, 2006 4:04 AM

Tagroom??? Blog said:

# April 26, 2006 2:39 AM

Quan said:

I have a problem as follow and i think you can help because i have a feeling that it is similar to the topic that you post.

I use For ...Next to generate ImageButtons with IDs are Items' IDs loaded from Database. These buttons are AddToCart buttons.

Thus, I dont know how to make them handle events AddItemToCart because they are created at run time. I can't use them to write a Sub as i can do with Buttons dragged and dropped. Examples : if i have a button dragged and dropped with ID "Button1" , all i have to do is writing a Sub  "Button1_Click"

So How can I write a sub handled by button whose ID is assigned at Run Time ?

# November 26, 2006 7:36 AM

Quan said:

I have a problem as follow and i think you can help because i have a feeling that it is similar to the topic that you post.

I use For ...Next to generate ImageButtons with IDs are Items' IDs loaded from Database. These buttons are AddToCart buttons.

Thus, I dont know how to make them handle events AddItemToCart because they are created at run time. I can't use them to write a Sub as i can do with Buttons dragged and dropped. Examples : if i have a button dragged and dropped with ID "Button1" , all i have to do is writing a Sub  "Button1_Click"

So How can I write a sub handled by button whose ID is assigned at Run Time ?

Could you please email solution to my Email : Tobenumb@yahoo.com .

Thanks for your help

# November 26, 2006 7:38 AM

pvanooijen said:

You cannot add eventhandler by declaring a handles. You have to add them from code. As explained in

http://codebetter.com/blogs/peter.van.ooijen/archive/2005/08/03/130245.aspx

What I would do is write one handling method. It can check which button triggered the event by inspecting the sender.

# November 27, 2006 6:24 AM

Priyanka said:

I too faced a similar problem..

Thanks for your help

:-)

# April 23, 2007 8:14 AM

Marvin Fray said:

If it possible to find out what eventHandlers are handling an event e.g. ...

let say I have a function which is passed an object of type button. When I recieve the button object I want to remove any eventhandlers that are currently handling the Click event. Is there a way of finding out what handlers have been added to this event.

Thanks in advance for your help.

# April 25, 2007 12:24 PM

pvanooijen said:

There is a way to find out.

The delegate has methods to inspect the list of eventhandlers subscribed. You can remove handlers using -=

I havn't done any of that myself but Jeffrey Richter has written some good stuff on it.

But most of that is hardcore C#

# April 26, 2007 4:41 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!