Name
WithEvents Keyword
Syntax
Dim|Private|Public WithEventsobjVarNameAsobjectType
-
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
WithEventskeyword can only be used in an object or class module.An object-variable declaration using the
WithEventskeyword 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
WithEventskeyword.WithEventsis only valid when used to declare an object variable.You cannot use
WithEventswhen declaring a generic Object type.Unlike other variable declarations, the
Askeyword is mandatory.There is no limit to the number of object variables that can refer to the same object using the
WithEventskeyword; they will all respond to that object’s events.You cannot create an array variable that ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access