Skip to content

Latest commit

 

History

History
55 lines (39 loc) · 2.34 KB

addhandler-statement.md

File metadata and controls

55 lines (39 loc) · 2.34 KB
description title ms.date f1_keywords helpviewer_keywords ms.assetid
Learn more about: AddHandler Statement
AddHandler Statement
07/20/2015
vb.AddHandlerMethod
addhandler
vb.addhandler
AddHandler statement [Visual Basic]
cfe69799-2a0f-42c0-a99e-09fed954da01

AddHandler Statement

Associates an event with an event handler at run time.

Syntax

AddHandler event, {AddressOf eventhandler | expression }

Parts

Part Description
event The name of the event to handle.
eventhandler The name of a procedure that handles the event.
expression A lambda expression that handles the event.

The parts AddressOf eventhandler and expression are mutually exclusive.

Remarks

The AddHandler and RemoveHandler statements allow you to start and stop event handling at any time during program execution.

The signature of the new event handler (the eventhandler procedure or the expression lambda) must match the signature of the event event.

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

A handler added with an explicit lambda CANNOT be removed later (using RemoveHandler). Indeed, if the lambda is not given a name, it is not possible to reference it later. But assigning the lambda to a variable and adding the handler through this variable allows to remove the handler using this variable.

Note

For custom events, the AddHandler statement invokes the event's AddHandler accessor. For more information on custom events, see Event Statement.

Example

[!code-vbVbVbalrEvents#17]

See also