Event Procedures


When an object in Visual Basic recognizes that an event has occurred, it automatically invokes the event procedure with the name corresponding to the event. Because the name establishes an association between the Object and the code, event procedures are said to be attached to Forms and Controls.

Ø An event procedure for a Control combines the Control’s actual name (specified in the Name property), an underscore (_), and the event name. For instance, if you want a command button named MyButton to invoke an event procedure when it is clicked, you have to use the procedure MyButton_Click.

Ø An event procedure for a Form combines the word “Form,” an underscore, and the event name. If you want a Form to invoke an event procedure when it is clicked, you have to use the procedure Form_Click. (Like controls, Forms do have unique names, but they are not used in the names of event procedures.) If you are using the MDI Form, the event procedure combines the word “MDIForm,” an underscore, and the event name, as in MDIForm_Load.

All event procedures use the same general syntax.

Syntax for a Control event

Private Sub Controlname_eventname (arguments )
statementblock
End Sub

Syntax for a form event

Private Sub Form_eventname (arguments)
statementblock
End Sub

No comments:

Post a Comment