Difference between revisions of "TTimer"

From Lazarus wiki
Jump to navigationJump to search
m (typo)
Line 2: Line 2:
  
 
'''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]].
 
'''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]].
 +
 +
== 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}})
 +
  
 
== See also ==
 
== See also ==

Revision as of 20:14, 9 November 2019

English (en) suomi (fi) français (fr) русский (ru)

TTimer 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 TCustomTimer.

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)


See also


LCL Components
Component Tab Components
Standard TMainMenu • TPopupMenu • TButton • TLabel • TEdit • TMemo • TToggleBox • TCheckBox • TRadioButton • TListBox • TComboBox • TScrollBar • TGroupBox • TRadioGroup • TCheckGroup • TPanel • TFrame • TActionList
Additional TBitBtn • TSpeedButton • TStaticText • TImage • TShape • TBevel • TPaintBox • TNotebook • TLabeledEdit • TSplitter • TTrayIcon • TControlBar • TFlowPanel • TMaskEdit • TCheckListBox • TScrollBox • TApplicationProperties • TStringGrid • TDrawGrid • TPairSplitter • TColorBox • TColorListBox • TValueListEditor
Common Controls TTrackBar • TProgressBar • TTreeView • TListView • TStatusBar • TToolBar • TCoolBar • TUpDown • TPageControl • TTabControl • THeaderControl • TImageList • TPopupNotifier • TDateTimePicker
Dialogs TOpenDialog • TSaveDialog • TSelectDirectoryDialog • TColorDialog • TFontDialog • TFindDialog • TReplaceDialog • TTaskDialog • TOpenPictureDialog • TSavePictureDialog • TCalendarDialog • TCalculatorDialog • TPrinterSetupDialog • TPrintDialog • TPageSetupDialog
Data Controls TDBNavigator • TDBText • TDBEdit • TDBMemo • TDBImage • TDBListBox • TDBLookupListBox • TDBComboBox • TDBLookupComboBox • TDBCheckBox • TDBRadioGroup • TDBCalendar • TDBGroupBox • TDBGrid • TDBDateTimePicker
Data Access TDataSource • TCSVDataSet • TSdfDataSet • TBufDataset • TFixedFormatDataSet • TDbf • TMemDataset
System TTimer • TIdleTimer • TLazComponentQueue • THTMLHelpDatabase • THTMLBrowserHelpViewer • TAsyncProcess • TProcessUTF8 • TProcess • TSimpleIPCClient • TSimpleIPCServer • TXMLConfig • TEventLog • TServiceManager • TCHMHelpDatabase • TLHelpConnector
Misc TColorButton • TSpinEdit • TFloatSpinEdit • TArrow • TCalendar • TEditButton • TFileNameEdit • TDirectoryEdit • TDateEdit • TTimeEdit • TCalcEdit • TFileListBox • TFilterComboBox • TComboBoxEx • TCheckComboBox • TButtonPanel • TShellTreeView • TShellListView • TXMLPropStorage • TINIPropStorage • TJSONPropStorage • TIDEDialogLayoutStorage • TMRUManager • TStrHolder
LazControls TCheckBoxThemed • TDividerBevel • TExtendedNotebook • TListFilterEdit • TListViewFilterEdit • TLvlGraphControl • TShortPathEdit • TSpinEditEx • TFloatSpinEditEx • TTreeFilterEdit • TExtendedTabControl •
RTTI TTIEdit • TTIComboBox • TTIButton • TTICheckBox • TTILabel • TTIGroupBox • TTIRadioGroup • TTICheckGroup • TTICheckListBox • TTIListBox • TTIMemo • TTICalendar • TTIImage • TTIFloatSpinEdit • TTISpinEdit • TTITrackBar • TTIProgressBar • TTIMaskEdit • TTIColorButton • TMultiPropertyLink • TTIPropertyGrid • TTIGrid
SQLdb TSQLQuery • TSQLTransaction • TSQLScript • TSQLConnector • TMSSQLConnection • TSybaseConnection • TPQConnection • TPQTEventMonitor • TOracleConnection • TODBCConnection • TMySQL40Connection • TMySQL41Connection • TMySQL50Connection • TMySQL51Connection • TMySQL55Connection • TMySQL56Connection • TMySQL57Connection • TSQLite3Connection • TIBConnection • TFBAdmin • TFBEventMonitor • TSQLDBLibraryLoader
Pascal Script TPSScript • TPSScriptDebugger • TPSDllPlugin • TPSImport_Classes • TPSImport_DateUtils • TPSImport_ComObj • TPSImport_DB • TPSImport_Forms • TPSImport_Controls • TPSImport_StdCtrls • TPSCustomPlugin
SynEdit TSynEdit • TSynCompletion • TSynAutoComplete • TSynMacroRecorder • TSynExporterHTML • TSynPluginSyncroEdit • TSynPasSyn • TSynFreePascalSyn • TSynCppSyn • TSynJavaSyn • TSynPerlSyn • TSynHTMLSyn • TSynXMLSyn • TSynLFMSyn • TSynDiffSyn • TSynUNIXShellScriptSyn • TSynCssSyn • TSynPHPSyn • TSynTeXSyn • TSynSQLSyn • TSynPythonSyn • TSynVBSyn • TSynAnySyn • TSynMultiSyn • TSynBatSyn • TSynIniSyn • TSynPoSyn
Chart TChart • TListChartSource • TRandomChartSource • TUserDefinedChartSource • TCalculatedChartSource • TDbChartSource • TChartToolset • TChartAxisTransformations • TChartStyles • TChartLegendPanel • TChartNavScrollBar • TChartNavPanel • TIntervalChartSource • TDateTimeIntervalChartSource • TChartListBox • TChartExtentLink • TChartImageList
IPro TIpFileDataProvider • TIpHtmlDataProvider • TIpHttpDataProvider • TIpHtmlPanel
Virtual Controls TVirtualDrawTree • TVirtualStringTree • TVTHeaderPopupMenu