Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

VB's Clock: The Timer Control

The Timer control acts unlike any other control you've seen so far. The Timer control always works in the background, and the user never sees the timer on the form. You will see the Timer control during design time because you need to be able to select the control and change its properties. Nevertheless, the timer's purpose is to work in the background, triggering an event every once in a while according to the clock ticks.

Your computer has an internal clock to keep things running smoothly. The hardware requires an accurate clock for memory refreshes and CPU cycle coordination efforts. Software such as Visual Basic can tap into the internal clock and use its timing to control certain time-based events that your application may need to perform.

Figure 11.8 shows the Timer control as it appears when you place the control on a form. The Timer control supports only seven properties because the Timer control never appears on the form at runtime. Therefore, the control has no need for many of the style and size properties used for other controls that the user sees.

11fig08.gif

Figure 11.8 The Timer control appears on the form only at design time.

You can place the timer out of the way of your form's other controls because its physical location is trivial. After you place the timer on the form, you should set its Interval property; Interval is the most important timer property. The Interval property contains a value that must range from 1 to 65,535. The value is in milliseconds (or thousandths of a second), so an Interval value of 500 would equate to half a second. The Timer control generates only one event: the Timer event. The Timer control triggers a Timer event after each interval of time goes by. Therefore, if you named a Timer control tmrClock, and if you set the control's Interval property to 1000, Visual Basic would execute the tmrClock_Timer() event procedure approximately every second.

A millisecond is one-thousandth of a second.

If you need an interval that's larger than the 65,535 Interval value allows (this maximum Interval value provides only about a 10-second interval), insert some If logic at the top of the Timer() event procedure that checks to see if the required amount of time has passed since the last interval. (To do this, you will need some of the time functions described in Hour 14, "Built-In Functions Save Time." )

Share ThisShare This

Informit Network