Name
WithEvents Keyword
Syntax
Dim|Private|Public WithEventsobjVarName
AsobjectType
-
objVarName
(required; String) The name of any object variable that refers to an object that exposes events
-
objectType
(required; any object type other than the generic Object) The ProgID of a referenced object
Description
The WithEvents
keyword informs VB that the object
being referenced exposes events for which you intend to provide event
handlers.
When you declare an object variable using
WithEvents
, an entry for the object variable is
placed in the code window’s drop-down Object List,
and a list of the events available to the object variable is placed
in the code window’s drop-down Procedures List. You
can then write code event handlers for the object variable.
Rules at a Glance
An object-variable declaration using the
WithEvents
keyword can only be used in an object or class module.An object-variable declaration using the
WithEvents
keyword should only be placed in the Declarations section of the object module.Any ActiveX object or class module that exposes events can be used with the
WithEvents
keyword.WithEvents
is only valid when used to declare an object variable.You cannot use
WithEvents
when declaring a generic Object type.Unlike other variable declarations, the
As
keyword is mandatory.There is no limit to the number of object variables that can refer to the same object using the
WithEvents
keyword; they will all respond to that object’s events.You cannot create an array variable that ...
Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.