LESSON7 - The TIMER Contro1
LESSON7 - The TIMER Contro1
The Timer control allows you to perform a task at a specified interval or to wait for a specified length of time. Note
the following facts about the Timer control:
- It is not visible to the user at run-time
- The actions that you want to happen at the specified interval are coded in the Timer's Timer event.
- You specify the interval that you want actions to occur in the Timer control's Interval property. The
value is specified in milliseconds (1000 milliseconds = 1 second). In the example, the value of 250 will
cause the Timer event to fire every quarter of a second.
- You turn the timer "on" or "off" by setting its Enabled property to True or False, respectively. The
Enabled property is set to True by default.
Property Description
Name Defaults to Timer1 if you have one Timer control, Timer2 if you have two Timer
controls, and so on.
Property Description
Enabled Turns a Timer on and off. The default is True, or on.
Index Reports the position of a specific Timer control within a control array.
Interval Determines, in milliseconds, when a Timer event will fire (1 second = 1000).
Left The position of the left edge of a Timer.
Tag The Tag property is like having a variable built right into the control. It's a catch-all
property to which you can assign any necessary data to your program and that you want
to persist throughout the life of your program.
Top The position of the top edge of a Timer.
-
EXAMPLE1
a) Start a new VB project and place two command buttons and a timer control on the form. Set the properties of
the controls as follows:
c) Code the Click event for the two command buttons and the Timer event for the timer control as follows:
mintCount = 0
Cls
tmrTest.Enabled = True
End Sub
Private Sub cmdStop_Click()
tmrTest.Enabled = False
End Sub
End Sub
d) Run the program. Click the "Start Timer" button. Messages will appear on the form every quarter of a second.
They will stop when you click the "Stop Timer" button. A sample run might look like this:
Timer Example 2
The timer control
The Timer control can be used to make an object on your screen appear to blink. This is done by toggling
the Visible property of the object on and off at short intervals. The Visible property is available to nearly all
controls – it is a Boolean property that determines whether or not the control is visible to the user at run time by
setting its value to True or False (it will always be visible at design time). To build a simple demo, perform the
following steps.
Run the project. The label will blink. After an appropriate period of observation, close the form.
e) Start a new VB project and place two command buttons and a timer control on the form. Set the properties of
the controls as follows:
Dim starttime
Private Sub Command1_Click()
starttime = Now
Timer1.Enabled = True
End Sub