Events in Visual Basic
6 November 2011
What is an Event?
• An action caused by the user from the keyboard or the mouse.
•Click - Double Click - Mouse Down - Mouse Up
•Mouse Move - Change - Got Focus - Lost Focus
•Load - Activate - Unload - Resize
• A timeout caused by the Interval in a Timer Control.
What Services an Event?
Visual Basic instructions written by the programmer service
the event.
Defined by the T O E chart
• For each Event.
•There is an Object to receive the Event.
•There must be VB code to accomplish the Task.
T O E Example
Object Task Event
Command Calculate Click
Button Inches From
Twips found in
Text Box.
Display Inches
in Label Box
T O E Example
Implies
•A Text Box to allow input of Twips
•A Label Box to receive the converted Inches
•A Command Button to accept the Click Event and contain
the necessary code.
Form has been Loaded
Here is a VB Form waiting for
the user to enter a number of
TWIPs into the Text Box.
User Enters Data
The User then types in a
number of TWIPs.
Here 2880 has been entered
But there is no Event associated
with this action, so nothing
happens.
User Causes an Event
The User then causes a Click
Event on the Calculate
Command Button.
Objects Code is Invoked
This causes the VB code
associated with the Click Event
to be invoked.
Private Sub Command1_Click()
' Convert entered Twips into Inches
' 1440 Twips equals one Inch.
Label2.Caption = Format(Val(Text1.Text) / 1440, "0.00") & " Inches"
End Sub
The Task is Accomplished
The code moves the formatted
answer into the Label Box.
Private Sub Command1_Click()
' Convert entered Twips into Inches
' 1440 Twips equals one Inch.
Label2.Caption = Format(Val(Text1.Text) / 1440, "0.00") & " Inches"
End Sub