Partilhar via


Visual Basic Concepts

Summary of Declaring, Raising, and Handling Events

To add an event to a class and then use the event, you must:

  • In the Declarations section of the class module that defines the class, use the Event statement to declare the event with whatever arguments you want it to have. Events are always Public.

    Note   Events cannot have named arguments, optional arguments, or ParamArray arguments. Events do not have return values.

  • At appropriate places in the class module's code, use the RaiseEvent statement to raise the event, supplying the necessary arguments.

  • In the Declarations section of the module that will handle the event, add a variable of the class type, using the WithEvents keyword. This must be a module-level variable.

  • In the left-hand drop down of the code window, select the variable you declared WithEvents.

  • In the right-hand drop down, select the event you wish to handle. (You can declare multiple events for a class.)

  • Add code to the event procedure, using the supplied arguments.

For More Information   Details and code examples are provided in "Adding Events to a Class."