Delphi Tutorials »

Timed Dialogs

Suppose that you want an "error" (or "warning") dialog- box that closes automatically, say after 60 seconds. You want to call that dialog from your code just like you call Delphi's dialogs, for example call like this:

  'Unexpected value in edit-box AGE. Please correct.'); 

Here's a quick and simple solution:

  1. Add a new form to your project and name the form TimedDlg. Save the new unit as TimedDlgU.
  2. Set the form's BorderStyle property to bsDialog.
  3. Add three components:
    - a TTimer; set its property Interval to 60000 (that's 60000 milliseconds); set its property Enabled to False;
    - a TMemo;
    - a TBitButton; set its property Kind to bkOK.
  4. In the PUBLIC section of unit TimedDlgU, declare a procedure with the name Execute:
    unit TimedDlgU;
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, Buttons;
      TTimedDlg = class(TForm)
      btnOK: TBitBtn;
        { Private declarations }
        { Public declarations }
        procedure Execute(Title, Contents:
    string); // added by you
      TimedDlg: TTimedDlg;
  5. In the implementation section of your new unit, add the following code:
  6. procedure TTimedDlg.Execute(Title, Contents: string);
      Caption := Title;       // set the caption of the
      Memo1.Text := Contents; // set the text of the memo
      Timer1.Enabled := True; // start the timer
  7. Double click on the TTimer component. Delphi will write an "OnTimer" event handler in your code.
  8. Complete this "OnTimer" procedure as follows:
    procedure TTimedDlg.Timer1Timer;
      Timer1.Enabled := False; // stop the timer
      ModalResult := mrOK;     // close the form

The procedure Execute shows the form in a "modal" way, that means: the dialog form appears on top of the other window(s) and your application doesn't continue before the dialog form is closed.
When the user clicks the OK button, the dialog form is closed.
But if the user doesn't click the OK button within 60 seconds, this form is closed "automatically".

Note: don't forget to add the unit TimedDlgU to the uses directive in each unit from where you call TimedDlg.Execute, like this:

  uses ..., ..., TimedDlgU;


TOP  DC Library  FAQ  Crash Course Delphi  Tips  Source Code  Downloads  Links

© Copyright DelphiLand 1999-2019