Handles 
This page is specific to:.NET Framework Version:1.12.03.03.54.0
Visual Basic Language Reference 
Handles 

Declares that a procedure handles a specified event.

proceduredeclaration Handles eventlist
Parts

proceduredeclaration

The Sub procedure declaration for the procedure that will handle the event.

eventlist

List of the events for proceduredeclaration to handle. The events must be raised by either the base class for the current class, or by an object declared using the WithEvents keyword.

Remarks

Use the Handles keyword at the end of a procedure declaration to cause it to handle events raised by an object variable declared using the WithEvents keyword. The Handles keyword can also be used in a derived class to handle events from a base class.

The signature of the procedure must match the signatures of each event in eventlist.

The Handles keyword and the AddHandler statement both allow you to specify that particular procedures handle particular events, but there are differences. Use the Handles keyword when defining a procedure to specify that it handles a particular event. The AddHandler statement connects procedures to events at run time. For more information, see AddHandler Statement.

For custom events, the application invokes the event's AddHandler accessor when it adds the procedure as an event handler. For more information on custom events, see Event Statement.

Example

Public Class ContainerClass
    ' Module or class level declaration.
    WithEvents Obj As New Class1

    Public Class Class1
        ' Declare an event.
        Public Event Ev_Event()
        Sub CauseSomeEvent()
            ' Raise an event.
            RaiseEvent Ev_Event()
        End Sub
    End Class

    Sub EventHandler() Handles Obj.Ev_Event
        ' Handle the event.
        MsgBox("EventHandler caught event.")
    End Sub

    ' Call the TestEvents procedure from an instance of the ContainerClass 
    ' class to test the Ev_Event event and the event handler.
    Public Sub TestEvents()
        Obj.CauseSomeEvent()
    End Sub
End Class

The following example demonstrates how a derived class can use the Handles statement to handle an event from a base class.

Public Class BaseClass
    ' Declare an event.
    Event Ev1()
End Class
Class DerivedClass
    Inherits BaseClass
    Sub TestEvents() Handles MyBase.Ev1
        ' Add code to handle this event.
    End Sub
End Class

See Also

Reference

WithEvents
AddHandler Statement
RemoveHandler Statement
Event Statement
RaiseEvent Statement

Concepts

Events and Event Handlers

Community Content

The Handles keyword accepts multiple events
Added by:supoch14
The Handles keyword can accept more than one event, which is why the documentation says "List of the events for proceduredeclaration to handle". To handle multiple event, enter them as a comma separated list:

Sub EventHandler() Handles Obj.Ev_Event, Obj2.Ev_Event
' Handle the event.
MsgBox("EventHandler caught event.")
EndSub

Be care which events you handle when specifying a list - if you add an event for an object which overrides an event for it's base object, you event handling function could fire more than once. ASP.NET Example: Your Page_Load event can fire twice in VB.NET when your Page_Load signature looks something like this:

Private

Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load, Me.Load

Because the MyBase object is a reference to the Page object which the aspx page inherits from, the Page_Load event will fire twice, once for MyBase.Load and once for Me.Load. The standard signature is to only handle MyBase.Load, calling Me.Load also is therefor unnecessary.
© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View