JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Timers

Timers are also very useful controls, because they let you create periodic events. Strictly speaking, timers are no longer controls but components, and they do not appear in a window at run time. At design time, they appear in the component tray underneath the form you've added them to. There's a timer at work behind the scenes in the Timers example in Figure 8.7, which shows a clock (using a label control) and lets the user set an alarm setting—as soon as the current time matches the alarm setting, the program will start to beep until you click the "Alarm off" radio button.

Click To expand
Figure 8.7: A timer at work.

Windows timers are designed for a single-threaded (as opposed to multithreaded) environment; you set how often you want the timer to generate Tick events by setting the Interval property (in milliseconds, one thousandths of a second). Each time a Tick event happens, you can execute code in a handler for this event, just as you would for any other event.

Tip 

In VB6 and before, you could set a timer's Interval property to 0 to disable the timer, but the minimum possible value for this property is now 1—you now use the Enabled property to turn timers on and off. You also can use the new Start and Stop methods to start and stop a timer.

This control is a useful one for, among other things, creating clocks like the one you see in Figure 8.7. However, you should use it with care. One of the guiding principles behind Windows programming is that the user should direct the action as much as possible; if you find yourself using a timer just to wrest control away from the user, think twice about what you're doing.

That's enough detail for the moment—it's time to get to the Immediate Solutions section to handle point-by-point issues.

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor