EVENTS

A control or other object raises an event to let the program know about some change in circumstances. Sometimes raising an event is also called firing the event. Specific control classes provide events that are relevant to their special purposes. For example, the Button control provides a Click event to let the program know when the user clicks the button.

The program responds to an event by executing code in an event handler that catches the event and takes whatever action is appropriate. Each event defines its own event handler format and determines the parameters that the event handler will receive. Often, these parameters give additional information about the event.

For example, when part of the form is covered and exposed, the form raises its Paint event. The Paint event handler takes as a parameter an object of type PaintEventArgs named e. That object’s Graphics property is a reference to a Graphics object that the program can use to redraw the form’s contents.

Some event handlers take parameters that are used to send information about the event back to the object that raised it. For example, the Form class’s FormClosing event handler has a parameter of type FormClosingEventArgs. That parameter is an object that has a property named Cancel. If the program sets Cancel to True, the Form cancels the FormClosing event and remains open. For example, the event handler can verify that the data entered by the user was properly formatted. If the values don’t make sense, the ...

Get Visual Basic 2012 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.