TTimer: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: TTimer
| Old | New | Line | |
|---|---|---|---|
| 1 | + | **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 [[doc:lcl/customtimer/tcustomtimer.html|TCustomTimer]]. It is defined in the ExtCtrls unit. | |
| 2 | + | ||
| 3 | + | ## Countdown calculation example | |
| 4 | + | ||
| 5 | + | - Drop a [[TLabel|label]], [[TShape|shape]] and timer components on main [[TForm|form]] | |
| 6 | + | - Doubleclick this Timer1 on the form (the default handler: OnTimer is created for Timer1, the source text editor opens). | |
| 7 | + | - Add following [[Source code|source code]] in the event handler: | |
| 8 | + | ||
| 9 | + | ``` pascal | |
| 10 | + | procedure TForm1.Timer1Timer(Sender: TObject); | |
| 11 | + | begin | |
| 12 | + | Label1.Caption := Format('%d sec',[start]); | |
| 13 | + | Dec(start); | |
| 14 | + | if (start < 5) then Shape1.Brush.Color:=clYellow; | |
| 15 | + | if (start < 0) then begin | |
| 16 | + | Timer1.Enabled := False; | |
| 17 | + | Shape1.Brush.Color:=clGreen; | |
| 18 | + | Label1.Caption := 'Finished!'; | |
| 19 | + | end; | |
| 20 | + | end; | |
| 21 | + | ``` | |
| 22 | + | ||
| 23 | + | - Create the OnCreate event handler of Form1 (go in the Object Inspector to the event OnCreate and click the button \[...\]). | |
| 24 | + | - Complete the source code as follows: | |
| 25 | + | ||
| 26 | + | ``` pascal | |
| 27 | + | ||
| 28 | + | procedure TForm1.FormCreate(Sender: TObject); | |
| 29 | + | begin | |
| 30 | + | Caption := 'Countdown calculation'; | |
| 31 | + | Timer1.Interval := 1000; | |
| 32 | + | Timer1.Enabled := True; | |
| 33 | + | Label1.Caption := ''; | |
| 34 | + | Shape1.Shape := stCircle; | |
| 35 | + | Shape1.Brush.Color:=clRed; | |
| 36 | + | start := 20; | |
| 37 | + | end; | |
| 38 | + | ``` | |
| 39 | + | ||
| 40 | + | - In the source editor, entering [[Class|class]] *TForm1* in the section [[Private|private]]. | |
| 41 | + | - Add code **`start: integer;`** | |
| 42 | + | - Start [[Program|program]] (with Key `F9`) | |
| 43 | + | ||
| 44 | + | ## Notes | |
| 45 | + | ||
| 46 | + | - In order to restart the counter of the timer use the following code: | |
| 47 | + | ||
| 48 | + | ``` pascal | |
| 49 | + | Timer1.Enabled := False; | |
| 50 | + | Timer1.Enabled := True; | |
| 51 | + | ``` | |
| 52 | + | ||
| 53 | + | Each time the code is executed the timer shall start counting from the beginning without the OnTimer event being triggered. | |
| 54 | + | ||
| 55 | + | ## See also | |
| 56 | + | ||
| 57 | + | - [[doc:lcl/extctrls/ttimer.html|TTimer doc]] | |
| 58 | + | - [[TIdleTimer]] | |
| 59 | + | ||
| 60 | + | --- | |
| 61 | + | *From the Free Pascal wiki, page "TTimer", as it stood on 2023-01-10 after a change by Dirkjvanderveen, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
