O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

WithEvents Keyword

Syntax

Dim|Private|Public WithEvents objVarName As objectType
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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required