The timer is a non-visual component that can trigger ontimer events on a regular basis to complete analog clock, system latency, countdown, and other work. In the system tab.
1. main attributes of Timer
(1) enabled attribute: When the value is true, enable the timer; otherwise, disable the timer. The default value is true.
(2) interval attribute: controls the interval of ontimer event triggering, in milliseconds. Setting interval to 0 is equivalent to disabling the timer. Default Value: 1000 ms (1 second ).
2. Major timer events
Timer only has one ontimer event. When timer is enabled, Timer triggers the ontimer event and runs the program at the time specified by the interval attribute.
Example: Countdown
Unit unit1;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, spin, extctrls, stdctrls, mask, buttons;
Type
Tform1 = Class (tform)
Timer1: ttimer;
Maskedit1: tmaskedit;
Bitbtn1: tbitbtn;
Procedure timer1timer (Sender: tobject );
Procedure formcreate (Sender: tobject );
Procedure bitbtn1click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;
VaR
Form1: tform1;
M, S: integer;
Implementation
{$ R *. DFM}
Procedure tform1.timer1timer (Sender: tobject );
Begin
S: = s-1;
If S =-1 then
Begin
S: = 59;
M: m-1;
If M =-1 then M: = 59;
End;
Maskedit1.text: = inttostr (m) +':'+ Inttostr (s );
Maskedit1.sellength: = 0;
End;
Procedure tform1.formcreate (Sender: tobject );
Begin
M: = 9; S: = 10; // initial time: 09: 10 seconds
Maskedit1.editmask: ='! 00: 00; 1; 0'; // Time format, 0 for non-input characters
Timer1.interval: = 1000;
End;
Procedure tform1.bitbtn1click (Sender: tobject );
Begin
Timer1.enabled: = Not timer1.enabled; // stop countdown
End;
End.