| title | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.topic | f1_keywords | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | translation.priority.ht | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Handles Clause (Visual Basic) |
2015-07-20 |
.net |
|
article |
|
|
|
1b051c0e-f499-42f6-acb5-6f4f27824b40 |
19 |
dotnet-bot |
dotnetcontent |
|
Handles Clause (Visual Basic)
Declares that a procedure handles a specified event.
Syntax
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, separated by commas. 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 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
[!code-vbVbVbalrEvents#2]
The following example demonstrates how a derived class can use the Handles statement to handle an event from a base class.
[!code-vbVbVbalrEvents#3]
Example
The following example contains two button event handlers for a WPF Application project.
[!code-vbVbVbalrEvents#41]
Example
The following example is equivalent to the previous example. The eventlist in the Handles clause contains the events for both buttons.
[!code-vbVbVbalrEvents#42]
See Also
WithEvents
AddHandler Statement
RemoveHandler Statement
Event Statement
RaiseEvent Statement
Events