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

Visual Basic
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.

Visual Basic
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

Concepts

Reference

Tags : events


Community Content

Đonny
2-dotted (nested) events with Handles

Sometimes it is possible to use Handles with nested objects (like ... Handles WithEventsVariable.Property.Event). But certain specific conditions must be met to achieve this.

  1. WithEventsVariable must be field declared WithEvents.
  2. Type of WithEventsVariable must be Class (not structure, not interface)
  3. The Property must be readonly property of WithEventsVariable class
  4. The Property must be docorated with <DesignerSerializationVisibility(DesignerSerializationVisibity.Content)> attribute
  5. The Event must be event of Property type
  6. Type of property must be either Interface or Class (no structure)

Intellisense support for thsi is limited. In case Handles does not work, you can still use AddHandler.

Example

This example needs form with docked FlowLayoutPanel FlowLayoutPanel1.

  

Imports System.ComponentModel

Public Class Form1

PublicSubNew()

InitializeComponent()

FlowLayoutPanel1.Controls.Add(cWithReadOnlyButton.Button)

EndSub

Private WithEvents cWithReadOnlyButton As New ClassWithReadOnlyButton

Private Sub cWithReadOnlyButton_Button_Click(ByVal sender As Button, ByVal e As EventArgs) _

Handles cWithReadOnlyButton.Button.Click

MsgBox( _

"You've clicked: " & sender.Text, _

MsgBoxStyle.Information, _

"2-dotted event on class")

End Sub

End Class

Friend Class ClassWithReadOnlyButton

Private _Button As New Button _

With {.Text = "Click me!", _

.AutoSize = True, _

.AutoSizeMode = AutoSizeMode.GrowAndShrink}

<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _

Public ReadOnly Property Button() As Button

Get

Return _Button

End Get

End Property

End Class


Page view tracker