Suppose that you want a "error" (or "warning") dialogbox 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:
TimedDlg.Execute('Warning',
'Unexpected value in edit-box AGE. Please correct.';
Here's a quick and simple solution:
- Add a new form to your project and call it frmTimedDlg. Save the new unit as TimedDlg.
- Set the form's BorderStyle property to bsDialog.
- 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.
- In the PUBLIC section of unit TimedDlg, declare a procedure with the name Execute:
unit TimedDlg;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons;
type
TfrmTimedDlg = class(TForm)
btnOK: TBitBtn;
private
{ Private declarations }
public
{ Public declarations }
procedure Execute(Title, Contents: string); // added by you
end;
var
frmTimedDlg: TfrmTimedDlg;
- In the implementation section of your new unit, add the following code:
procedure TfrmTimedDlg.Execute(Title, Contents: string);
begin
Caption := Title;
Memo1.Text := Contents;
ShowModal;
end;
- Double click on the TTimer component. Delphi will write an "OnTimer" event handler
in your code.
- Complete this "OnTimer" procedure as follows:
procedure TfrmTimedDlg.Timer1Timer;
begin
Timer1.Enabled := False; // stop the timer
ModalResult := mrOK; // close the form
end;
The function Execute shows the form in a "modal" way, that
means: the window will open on top of the other window(s) and your application doesn't continue before the dialog form is closed.
If 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 TimedDlg to the uses directive in each unit
where you call TimedDlg.Execute, like this:
uses ..., ..., TimedDlg;
|
|