Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-07

TFPTimer: how can I use it to closse a window?

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

Hi,

I've made a really simple application, that shows a simple dialog box with current time.

I want it to close itself after 15 seconds.

I tried to use TFPTimer, but there are no examples on documentation and so I have no idea how to use OnTimer property to call a procedure with TForm1.Close() instruction inside.

How can I do it?

Graeme Geldenhuys
to Unknown author

Op 2010-07-09 09:54, het geskryf:

How can I do it?

You don't say what GUI toolkit you are using, but either way, the usage should be very similar. Here is a working example of what you want using fpGUI Toolkit.

Basic usage:
* you set the interval in milliseconds
* you define the OnTimer event handler
* you start the timer via .StartTimer.
* Under Linux you need cthreads because the FPTimer using threading.

--------------------[ project1.pas ]-----------------------------
program project1;
{$mode objfpc}{$H+}
uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  Classes, fpg_main, fpg_form, fpg_label, fpg_button, fpTimer;
type
  TMainForm = class(TfpgForm)
    procedure ButtonPressed(Sender: TObject);
  public
     MainForm}
    Button1: TfpgButton;
     MainForm}
    procedure AfterCreate; override;
  end;
  TTimedForm = class(TfpgForm)
    procedure TimerFired(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClosing(Sender: TObject; var CloseAction: TCloseAction);
  private
     TimedForm}
    Label1: TfpgLabel;
     TimedForm}
    FTimer: TFPTimer;
  public
    constructor Create(AOwner: TComponent); override;
    procedure AfterCreate; override;
  end;
procedure TTimedForm.TimerFired(Sender: TObject);
begin
  Close;
end;
procedure TTimedForm.FormShow(Sender: TObject);
begin
  // only start the timer when the form is actually visible
  FTimer.StartTimer;
end;
procedure TTimedForm.FormClosing(Sender: TObject; var CloseAction:
TCloseAction);
begin
  CloseAction := caFree;
end;
constructor TTimedForm.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FTimer := TFPTimer.Create(self);
  FTimer.Interval := 15000; // in milliseconds
  FTimer.OnTimer  := @TimerFired;
  OnShow  := @FormShow;
  OnClose  := @FormClosing;
end;
procedure TTimedForm.AfterCreate;
begin
  {%region 'Auto-generated GUI code' -fold}
   TimedForm}
  Name := 'TimedForm';
  SetPosition(438, 398, 277, 95);
  WindowTitle := 'TimedForm';
  Hint := '';
  Label1 := TfpgLabel.Create(self);
  with Label1 do
  begin
    Name := 'Label1';
    SetPosition(68, 40, 148, 16);
    Alignment := taCenter;
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Show for 15 seconds';
  end;

TimedForm}
{%endregion}
end;

procedure TMainForm.ButtonPressed(Sender: TObject);
var
  frm: TTimedForm;
begin
  frm := TTimedForm.Create(nil);
  frm.Show;
end;
procedure TMainForm.AfterCreate;
begin
  {%region 'Auto-generated GUI code' -fold}
   MainForm}
  Name := 'MainForm';
  SetPosition(316, 186, 300, 250);
  WindowTitle := 'MainForm';
  Hint := '';
  Button1 := TfpgButton.Create(self);
  with Button1 do
  begin
    Name := 'Button1';
    SetPosition(116, 44, 80, 24);
    Text := 'Button';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 1;
    OnClick  := @ButtonPressed;
  end;

MainForm}
{%endregion}
end;

procedure MainProc;
var
  frm: TMainForm;
begin
  fpgApplication.Initialize;
  frm := TMainForm.Create(nil);
  try
    frm.Show;
    fpgApplication.Run;
  finally
    frm.Free;
  end;
end;
{$R *.res}
begin
  MainProc;
end.

----------------------------------------------------

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/