Parts

attrlist

Optional. List of attributes that apply to this event. Multiple attributes are separated by commas.

Public

Optional. Entities declared with the Public modifier have public access. There are no restrictions on the use of public entities. Events that do not specify an access modifier are declared as Public by default.

Private

Optional. Entities declared with the Private modifier have private access. A private entity is accessible only within its declaration context, including any nested entities.

Protected

Optional. Entities declared with the Protected keyword have protected access. They are accessible only from within their own class or from a derived class. Protected access can be specified only on members of classes. It is not a superset of friend access.

Friend

Optional. Entities declared with the Friend modifier have friend access. An entity with friend access is accessible only within the program that contains the entity declaration.

Protected Friend

Optional. Entities declared with the Protected Friend modifiers have the union of protected and friend accessibility.

Shadows

Optional. Indicates that this event shadows an identically named programming element in a base class. You can shadow any kind of declared element with any other kind. A shadowed element is unavailable from within the derived class that shadows it, unless the shadowing element is inaccessible, for example if it is Private.

eventname

Required. Name of the event; follows standard variable naming conventions.

Implements

Optional. Indicates that this event implements an event of an interface.

interfacename

The name of an interface.

interfaceeventname

The name of the event being implemented.

Each attribute in the attrlist part has the following syntax and parts:

Note You can declare event arguments just as you do arguments of procedures, with the following exceptions: events cannot have named arguments, or Optional arguments. Events do not have return values.

Example

The following example uses events to count off seconds during a demonstration of the fastest 100-meter race. The code illustrates all of the event-related methods, properties, and statements, including the RaiseEvent statement.

The class that raises an event is the event source, and the methods that process the event are the event handlers. An event source can have multiple handlers for the events it generates. When the class raises the event, that event is raised on every class that has elected to handle events for that instance of the object.

The example also uses a form (Form1) with a button (Command1), a label (Label1), and two text boxes (Text1 and Text2). When you click the button, the first text box displays "From Now" and the second starts to count seconds. When the full time (9.84 seconds) has elapsed, the first text box displays "Until Now" and the second displays "9.84"

The code for Form1 specifies the initial and terminal states of the form. It also contains the code executed when events are raised.

To use this example, open a new Windows Forms project, add a button named Button1, a label named Label1 and two text boxes, named TextBox1 and TextBox2, to the main form, named form1. Then right click the form and click View Code to open the code editor.

To simplify access to the Timer property, add an Imports statement as the first line of code above the Class Form1 statement.

Press F5 to run this example, and click the button labeled Click to start timer. The first text box displays "From Now" and the second starts to count seconds. When the full time (9.84 seconds) has elapsed, the first text box displays "Until Now" and the second displays "9.84".