[Lazarus] TTimer woes (newbie)

Howard Page-Clark hdpc at talktalk.net
Mon Mar 31 11:35:08 CEST 2014


On 31/03/2014 10:25, Michael Schnell wrote:
> On 03/31/2014 01:44 AM, Bob Axtell wrote:
>> can someone show a newbie how to use the two system timers?
>
> What do you mean by system timers ?

I think he means the TTimer and TIdleTimer found on the System page of 
the Component Palette.
If you drop one of each on the main form of a new project with a label 
and a memo, and add this code, it should give you an idea of how to use 
them.

-- code begin --

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
   SysUtils, Forms, ExtCtrls, StdCtrls;

type

   { TForm1 }

   TForm1 = class(TForm)
     IdleTimer1: TIdleTimer;
     LAverageSoFar: TLabel;
     MNumbers: TMemo;
     Timer1: TTimer;
     procedure FormCreate(Sender: TObject);
     procedure IdleTimer1Timer(Sender: TObject);
     procedure Timer1Timer(Sender: TObject);
   private
     FNumber: integer;
     FRunningTotal: integer;
     FCount: integer;
   end;

var
   Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
   Randomize;
   MNumbers.Clear;

   Timer1.Interval:=500;
   Timer1.OnTimer:=@Timer1Timer;
   Timer1.Enabled:=True;

   IdleTimer1.Interval:=1000;
   IdleTimer1.OnTimer:=@IdleTimer1Timer;
   IdleTimer1.Enabled:=True;
end;

procedure TForm1.IdleTimer1Timer(Sender: TObject);
begin
   LAverageSoFar.Caption:=
     Format('Average of random numbers so far is 
%n',[FRunningTotal/FCount]);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
   FNumber:=Random(101);
   MNumbers.Lines.Add(IntToStr(FNumber));
   Inc(FCount);
   Inc(FRunningTotal, FNumber);
end;

end.

-- code end --

Howard





More information about the Lazarus mailing list