An object in a VB program is a component of the user interface, such as a form (which represents the window in which the program executes) or a control (a button, list, checkbox, textbox, or some other component that is contained in the form). Every object has properties which define the characteristics of the object. One property that every object has is (Name) -- the name of the object. Every object is given a default name when you create it, but we always change the names, using a style of naming that helps us remember the type of the object. We start the name with an abbreviation of the object type:
Type of Object | Name Prefix | Example |
---|---|---|
button | btn | btnClear |
Label | lbl | lblTitle |
text box | txt | txtName |
While a program is running, the user can do many things, such as move the mouse, click the mouse button, enter text, or move to a different window. Each of these actions is called an event. Each event has two components: what the user did (click, type, etc.) and on which object did they perform the event (which button did they click on, which textbox did they type into). In Visual Basic you can write code that will perform actions in response to events. Some examples of events are:
Action | Event |
---|---|
click mouse button | Click |
double click mouse button | DblClick |
object is selected ("gets focus") | Enter |
object is no longer selected ("loses focus") | Leave |
contents of text box changes | TextChanged |
The code that performs actions in response to events is written in event procedures. Each event procedure contains the statements that execute when a particular event occurs on a particular object. Event procedure names have the form objectName_event, where objectName is the name of the object on which the event occurred and event is the type of the event. For example, an event procedure named cmdClear_Click will be executed when the user clicks on the button named cmdClear.
The event procedures are written in the code window. To open the code window, double click on the control for which you want to write the event procedure. The code window will open and it will contain the shell of the event procedure for that object:
Private Sub btnClear_Click(...) Handles btnClear.ClickEnd Sub
Private Sub btnMessage_Click(...) Handles btnMessage.Click ' this event procedure will write a message into the lblBanner label ' the message will be written in red ' it will be executed when the user clicks on the button named ' btnMessage lblBanner.ForeColor = Color.Red lblBanner.Text = "VB Sample Event Procedure!" End Sub
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2007