Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

Lesson XIII.

Timer

Objectives

To know how Timer works


To use Timer control in time-dependent applications

Notes
The Timer control works in the background you do not see it on the Form at runtime. The
primary purpose of a Timer is to trigger an event at a certain interval. The following are the
properties of a Timer.
Property
Enabled
Index
Interval

Description
Determines whether the Timer can respond to events.
Specifies the subscript of the control in a control array.
Specifies the number of milliseconds between calls to a
Timer controls Timer event. The value must be within the
range of 1 to 65535. This value is in millisecond (or a
thousandth of a second).

What can we do with a Timer? You may want to replace the caption property of a Label with another
quotation every 5 seconds. You may want to simulate animation by replacing the image of an
ImageBox every half a second. Or you may simply want to give a time-pressured quiz a sequence
of questions appears one after another at an interval of 10 seconds.
The Timer has one event, the Time event. The procedure <timer_name>.Timer( ) is executed every
<interval_value> milliseconds. The Timer control continues this until we disable the control by
setting the Enabled property to False.

Lesson in Action
Lets have some fun and create a small animation. Animation involves a series of still images
displayed in rapid sequence. We will need several images of an object to be animated at different
times. For this application, we will make a dog walk.
The following are the images that we will use:

The images are named dog1.wmf, dog2.wmf, dog3.wmf, dog4.wmf, and dog5.wmf. You will later
know why we have to make the names similar.
Follow these steps:
1. Create a new Project.
2. Drag an ImageBox to the Form and name it imgDog. Set the Picture property to dog1.wmf
(the initial snapshot of our dog). Resize the ImageBox to the size you want. Make sure that
the Stretch property is set to True so that the image resizes to the size of the ImageBox.

3. Drag a Timer to the Form.


4. What we want to do is to change the image every 80 milliseconds. Thus, set the Interval
5.

property of the Timer to 80.


We have already displayed dog1.wmf. We will have to display dog2.wmf, dog3.wmf, ,
dog5.wmf, then back to dog1.wmf. Add the following procedure:
Private Sub timTimer_Timer()
imgDog.Picture = LoadPicture (App.Path + "\dog" & Trim(Str(x)) & ".wmf")
If x < 5 Then
x=x+1
Else
x=1
End If
End Sub
What the above code does is to change the picture by calling the LoadPicture() function.
"\dog" & Trim(Str(x)) & ".wmf" specifies the filename of the image to be loaded and
displayed. Variables x serves as our counter. If x is currently one, then we load dog1.wmf. If
it is 2, then we load dog2.wmf. Had we not named the pictures such, we will have to write
the filenames lexically.
The first time timTimer_Timer() is called, we dog2.wmf should be loaded. Hence, we have to
initialize x to 2. We do this during Form_Load(). We will have to declare in the general area
with the Public keyword to make variable x accessible to all procedures.
Public x As Integer
Private Sub Form_Load()
x=2
End Sub

6. Run the application.


7. Save your work as Less13.vbp.

On your Own
1. Add a button to the Dog Animation application with caption Pause. If the user presses the
Pause button, the animation stops and the caption of the button changes to Play. If the user
clicks the Play button, animation resumes.
2. Create an application. Add a Label to the Form with the caption Timer. Set the Font
property to Arial, size 6. Drag a Timer control as well. Every second, add 3 points to the
Labels font size. When the font size grows to a value that will make the caption too big for the
Label, set the font size back to 6 points and start increasing the size once again.

imgMyPic.Picture = LoadPicture("man
SomeDirectory\SomeSubDir\MyPic.bmp")

"Picture" is the default property of the image control, so the above statement could have been
written as:

imgMyPic = LoadPicture("C:\SomeDirectory\SomeSubDir\MyPic.bmp")

You might also like