[lazarus] problem with timer....

Marc Weustink marc at dommelstein.net
Fri Oct 24 21:10:08 EDT 2003


At 01:52 25-10-2003, Prof. Roberto A. Berrospe Machin wrote:
>Hi everybody. :)
>
>Well im trying to make the TTIMER comonent working but with not result.
>I find the problem in the timer, but i dont know how to fix.
>
>In delphi this works fine....

I doubt it. For the following reasons:
* In all examples you are mixing regular procedure types and method types. 
That doesn't work (also not in Delphi)
* In the first example Mainform is it's own Owner. Or worse the mainform 
var is uninitialized.

In order to use events you *need* method types.
To keep your app running you need application.run.

So for both Delphi and Lazarus you should use Example 2 with a little 
modification. Both TESTTIMERX_ONTIMER and BUTTONTIMER_ONCLICK need to be a 
member of MAINFORMCLASS.

That is partially what you have done in example 3.

I tried it and it works, however it is a bit difficult to stop the timer.

Marc




**********
Example 1
**********

>USES
>FORMS,INTERFACES,CLASSES,BUTTONS,STDCTRLS,CONTROLS,DIALOGS,ExtCtrls;
>
>VAR
>   MAINFORM : TForm;
>   BUTTONTIMER : TButton;
>   TIMELABEL : TLabel;
>   TESTTIMERX : TTimer;
>
>
>Procedure TESTTIMERX_ONTIMER(SENDER : TOBJECT);
>Begin
>     TIMELABEL.Caption := 'Test';
>     Application.MessageBox('Test','test',1)
>End;
>
>Procedure BUTTONTIMER_ONCLICK(SENDER : TOBJECT);
>Begin
>     If (BUTTONTIMER.Caption='Start')  Then Begin
>         BUTTONTIMER.Caption := 'Stop';
>         TESTTIMERX.Interval := 2000;
>         TESTTIMERX.Enabled := TRUE;
>     End
>     Else Begin
>         BUTTONTIMER.Caption := 'Start';
>         TESTTIMERX.Enabled := FALSE;
>     End;
>End;
>
>
>
>{************************************************
>   MAIN PROCEDURE
>  ************************************************}
>BEGIN
>   MAINFORM := TForm.Create(MAINFORM);
>
>   MAINFORM.Caption  := 'Timer Test';
>   MAINFORM.Width  := 120;
>   MAINFORM.Height  := 100;
>
>   BUTTONTIMER := TButton.Create(nil);
>   BUTTONTIMER.Caption  := 'Start';
>   BUTTONTIMER.Top  := 25;
>   BUTTONTIMER.OnClick  := @BUTTONTIMER_ONCLICK;
>   BUTTONTIMER.Parent  := MAINFORM;
>
>   TIMELABEL := TLabel.Create(nil);
>   TIMELABEL.Caption  := 'Time';
>   TIMELABEL.AutoSize  := TRUE;
>   TIMELABEL.Top  := 5;
>   TIMELABEL.Left  := 5;
>   TIMELABEL.Parent  := MAINFORM;
>
>   TESTTIMERX := TTimer.Create(nil);
>   TESTTIMERX.Enabled  := FALSE;
>   TESTTIMERX.Interval  := 1000;
>   TESTTIMERX.OnTimer  := @TESTTIMERX_ONTIMER;
>
>   MAINFORM.SHow;
>END.


**********
Example 2
**********

>Well i have an error when try to use this in fpc... then i cant use Timer
>ussing this way...
>
>But theres another way thats works for me and is....
>
>
>
>
>USES
>FORMS,INTERFACES,CLASSES,BUTTONS,STDCTRLS,CONTROLS,DIALOGS,ExtCtrls;
>
>
>
>TYPE
>MAINFORMCLASS = Class(TForm)
>Public
>   BUTTONTIMER : TButton;
>   TIMELABEL : TLabel;
>   TESTTIMER : TTimer;
>   Constructor Create(AOwner: TComponent); override;
>End;
>
>
>VAR
>MAINFORM : MAINFORMCLASS;
>
>
>
>procedure TESTTIMER_ONTIMER(SENDER : TOBJECT);
>Begin
>     MAINFORM.TIMELABEL.Caption := 'Test';
>     Application.MessageBox('Test','test',1)
>End;
>
>Procedure BUTTONTIMER_ONCLICK(SENDER : TOBJECT);
>Begin
>     If (MAINFORM.BUTTONTIMER.Caption='Start')  Then Begin
>         MAINFORM.BUTTONTIMER.Caption := 'Stop';
>         MAINFORM.TESTTIMER.Interval := 2000;
>         MAINFORM.TESTTIMER.Enabled := TRUE;
>     End
>     Else Begin
>         MAINFORM.BUTTONTIMER.Caption := 'Start';
>         MAINFORM.TESTTIMER.Enabled := FALSE;
>     End;
>End;
>
>
>
>Constructor MAINFORMCLASS.Create(AOwner: TComponent);
>Begin
>      Inherited Create(AOwner);
>      Caption  := 'Timer Test';
>      Width  := 120;
>      Height  := 100;
>
>      BUTTONTIMER := TButton.Create(Self);
>      BUTTONTIMER.Caption  := 'Start';
>      BUTTONTIMER.Top  := 25;
>      BUTTONTIMER.OnClick  := @BUTTONTIMER_ONCLICK;
>      BUTTONTIMER.Parent  := MAINFORM;
>
>      TIMELABEL := TLabel.Create(Self);
>      TIMELABEL.Caption  := 'Time';
>      TIMELABEL.AutoSize  := TRUE;
>      TIMELABEL.Top  := 5;
>      TIMELABEL.Left  := 5;
>      TIMELABEL.Parent  := MAINFORM;
>
>      TESTTIMER := TTimer.Create(Self);
>      TESTTIMER.Enabled  := FALSE;
>      TESTTIMER.Interval  := 1000;
>      TESTTIMER.OnTimer  := @TESTTIMER_ONTIMER;
>End;
>
>
>BEGIN
>Application.Initialize;
>Application.CreateForm(MAINFORMCLASS , MAINFORM);
>Application.Run;
>END.
>
>
>
>
>Ussing this everything works fine.. minus the timer :(  im ussing this way
>for every object and every works fine) now im trying to use time and theres
>the unic component thats not work ussing this way....

**********
Example 3
**********



>But not end... i have continued trying and when i have tested this one....
>
>
>
>USES
>FORMS,INTERFACES,CLASSES,BUTTONS,STDCTRLS,CONTROLS,DIALOGS,ExtCtrls;
>
>
>
>TYPE
>MAINFORMCLASS = Class(TForm)
>Public
>   BUTTONTIMER : TButton;
>   TIMELABEL : TLabel;
>   TESTTIMER : TTimer;
>   procedure TESTTIMER_ONTIMER(SENDER : TOBJECT);
>   Constructor Create(AOwner: TComponent); override;
>End;
>
>
>VAR
>MAINFORM : MAINFORMCLASS;
>
>
>
>procedure MAINFORMCLASS.TESTTIMER_ONTIMER(SENDER : TOBJECT);
>Begin
>     MAINFORM.TIMELABEL.Caption := 'Test';
>     Application.MessageBox('Test','test',1)
>End;
>
>Procedure BUTTONTIMER_ONCLICK(SENDER : TOBJECT);
>Begin
>     If (MAINFORM.BUTTONTIMER.Caption='Start')  Then Begin
>         MAINFORM.BUTTONTIMER.Caption := 'Stop';
>         MAINFORM.TESTTIMER.Interval := 2000;
>         MAINFORM.TESTTIMER.Enabled := TRUE;
>     End
>     Else Begin
>         MAINFORM.BUTTONTIMER.Caption := 'Start';
>         MAINFORM.TESTTIMER.Enabled := FALSE;
>     End;
>End;
>
>
>
>Constructor MAINFORMCLASS.Create(AOwner: TComponent);
>Begin
>      Inherited Create(AOwner);
>      Caption  := 'Timer Test';
>      Width  := 120;
>      Height  := 100;
>
>      BUTTONTIMER := TButton.Create(Self);
>      BUTTONTIMER.Caption  := 'Start';
>      BUTTONTIMER.Top  := 25;
>      BUTTONTIMER.OnClick  := @BUTTONTIMER_ONCLICK;
>      BUTTONTIMER.Parent  := MAINFORM;
>
>      TIMELABEL := TLabel.Create(Self);
>      TIMELABEL.Caption  := 'Time';
>      TIMELABEL.AutoSize  := TRUE;
>      TIMELABEL.Top  := 5;
>      TIMELABEL.Left  := 5;
>      TIMELABEL.Parent  := MAINFORM;
>
>      TESTTIMER := TTimer.Create(Self);
>      TESTTIMER.Enabled  := FALSE;
>      TESTTIMER.Interval  := 1000;
>      TESTTIMER.OnTimer  := @TESTTIMER_ONTIMER;
>End;
>
>
>BEGIN
>Application.Initialize;
>Application.CreateForm(MAINFORMCLASS , MAINFORM);
>Application.Run;
>END.
>
>
>
>And voila This works!
>
>
>But only and only the timer can not handle the address of the procedures
>outside of the main Class; Every component (I HAVE TESTED) can use this
>procedures, But Timer NOt :(
>
>Theres a way to fix this problem in the main timer class?
>
>
>Thanks....
>
>_________________________________________________________________
>      To unsubscribe: mail lazarus-request at miraclec.com with
>                 "unsubscribe" as the Subject
>    archives at http://www.lazarus.freepascal.org/mailarchives






More information about the Lazarus mailing list