[lazarus] problem with timer....

Prof. Roberto A. Berrospe Machin binaryuniverse at binaryuniverse.com
Fri Oct 24 19:45:36 EDT 2003


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


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.



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


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






More information about the Lazarus mailing list