TTimer: Difference between revisions
From Free Pascal wiki
Jump to navigationJump to search
No edit summary |
m (Timer is part of the ExtCtrls instead of the StdCtrls unit) |
||
(8 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
'''TTimer''' [[image:ttimer.png]] is component on the [[System tab]] of the [[ | {{TTimer}} | ||
'''TTimer''' [[image:ttimer.png]] is component on the [[System tab]] of the [[Component Palette]] and delivers a timer with usually millisecond resolution. It inherits most of its properties from [[doc:lcl/customtimer/tcustomtimer.html|TCustomTimer]]. It is defined in the ExtCtrls unit. | |||
== Countdown calculation example == | |||
* Drop a [[TLabel|label]], [[TShape|shape]] and timer components on main [[TForm|form]] | |||
* Doubleclick this Timer1 on the form (the default handler: OnTimer is created for Timer1, the source text editor opens). | |||
* Add following [[Source code|source code]] in the event handler: | |||
<syntaxhighlight lang="pascal"> | |||
procedure TForm1.Timer1Timer(Sender: TObject); | |||
begin | |||
Label1.Caption := Format('%d sec',[start]); | |||
Dec(start); | |||
if (start < 5) then Shape1.Brush.Color:=clYellow; | |||
if (start < 0) then begin | |||
Timer1.Enabled := False; | |||
Shape1.Brush.Color:=clGreen; | |||
Label1.Caption := 'Finished!'; | |||
end; | |||
end; | |||
</syntaxhighlight> | |||
* Create the OnCreate event handler of Form1 (go in the Object Inspector to the event OnCreate and click the button [...]). | |||
* Complete the source code as follows: | |||
<syntaxhighlight lang="pascal"> | |||
procedure TForm1.FormCreate(Sender: TObject); | |||
begin | |||
Caption := 'Countdown calculation'; | |||
Timer1.Interval := 1000; | |||
Timer1.Enabled := True; | |||
Label1.Caption := ''; | |||
Shape1.Shape := stCircle; | |||
Shape1.Brush.Color:=clRed; | |||
start := 20; | |||
end; | |||
</syntaxhighlight> | |||
* In the source editor, entering [[Class|class]] ''TForm1'' in the section [[Private|<syntaxhighlight lang="pascal" inline>private</syntaxhighlight>]]. | |||
* Add code '''<syntaxhighlight lang="pascal" inline>start: integer;</syntaxhighlight>''' | |||
* Start [[Program|program]] (with Key {{keypress|F9}}) | |||
== Notes == | |||
* In order to restart the counter of the timer use the following code: | |||
<syntaxhighlight lang="pascal"> | |||
Timer1.Enabled := False; | |||
Timer1.Enabled := True; | |||
</syntaxhighlight> | |||
Each time the code is executed the timer shall start counting from the beginning without the OnTimer event being triggered. | |||
== See also == | == See also == | ||
* [[doc:lcl/extctrls/ttimer.html|TTimer doc]] | * [[doc:lcl/extctrls/ttimer.html|TTimer doc]] | ||
* [[TIdleTimer]] | |||
{{LCL Components}} | {{LCL Components}} |
Latest revision as of 18:28, 10 January 2023
│
English (en) │
suomi (fi) │
français (fr) │
русский (ru) │
TTimer is component on the System tab of the Component Palette and delivers a timer with usually millisecond resolution. It inherits most of its properties from TCustomTimer. It is defined in the ExtCtrls unit.
Countdown calculation example
- Drop a label, shape and timer components on main form
- Doubleclick this Timer1 on the form (the default handler: OnTimer is created for Timer1, the source text editor opens).
- Add following source code in the event handler:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Label1.Caption := Format('%d sec',[start]);
Dec(start);
if (start < 5) then Shape1.Brush.Color:=clYellow;
if (start < 0) then begin
Timer1.Enabled := False;
Shape1.Brush.Color:=clGreen;
Label1.Caption := 'Finished!';
end;
end;
- Create the OnCreate event handler of Form1 (go in the Object Inspector to the event OnCreate and click the button [...]).
- Complete the source code as follows:
procedure TForm1.FormCreate(Sender: TObject);
begin
Caption := 'Countdown calculation';
Timer1.Interval := 1000;
Timer1.Enabled := True;
Label1.Caption := '';
Shape1.Shape := stCircle;
Shape1.Brush.Color:=clRed;
start := 20;
end;
- In the source editor, entering class TForm1 in the section
private
. - Add code
start: integer;
- Start program (with Key F9)
Notes
- In order to restart the counter of the timer use the following code:
Timer1.Enabled := False;
Timer1.Enabled := True;
Each time the code is executed the timer shall start counting from the beginning without the OnTimer event being triggered.
See also