Udostępnij za pośrednictwem


How to: Add Events to a Class

You add events to a class by declaring them with the Events statement. The declaration includes the name of the event and the arguments it uses.

Adding an event to a class specifies that an object of this class can raise a specific event. To cause an event to actually happen, you must use the RaiseEvent statement. You can use the Handles keyword or the AddHandler statement to associate the event with an event-handler procedure. Events must be raised within the scope where they are declared. For example, a derived class cannot raise events inherited from a base class.

Note

Events cannot have return values, optional arguments, or ParamArray arguments.

To add an event to a class

  • 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. For example:

    Public Event PercentDone(ByVal Percent As Single, _
                             ByRef Cancel As Boolean)
    
    Public Event PercentDone(ByVal Percent As Single, _
                             ByRef Cancel As Boolean)
    

See Also

Tasks

Walkthrough: Declaring and Raising Events

Walkthrough: Handling Events

How to: Write Event Handlers

Concepts

Events and Event Handlers

Delegates and the AddressOf Operator

AddHandler and RemoveHandler

Reference

RaiseEvent Statement

Handles

AddHandler Statement

Other Resources

Polymorphism