Professional Documents
Culture Documents
Lesson XIII
Lesson XIII
Timer
Objectives
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.
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")