Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cancel a MessageDlg() if user doesn't respond

Status
Not open for further replies.

roo0047

Programmer
Jul 31, 2004
533
US
I had the following in my program's main form:
Code:
procedure TManagerMenu.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose:= MessageDlg('Exit Manager Menu?', mtConfirmation, [mbYes, mbNo], 0) = mrYes
end;
The problem was that, if the user never clicked "Yes" or "No", it never closed. I wanted to put a timer on the MessageDlg so that after some time, it closed anyway. The question was "How?" and I started to post it here, did a google search (so not to get flamed) but found nothing helpful. So, decided to try something before posting my question. Well, to my surprise it worked!

Here's my solution for anyone who may be posed with the same dilemma:
Code:
procedure TManagerMenu.Timer1Timer(Sender: TObject);
begin
  TimesUp:= true;
  PostMessage(Handle, wm_Close, 0, 0)
end;

procedure TManagerMenu.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var isDone: boolean;
begin
  if not TimesUp then begin
    Timer1.Enabled:= true;
    isDone:= MessageDlg('Exit DataEase emulator?', mtConfirmation, [mbYes, mbNo], 0) = mrYes;
    Timer1.Enabled:= false;
    CanClose:= isDone or TimesUp;
  end else
    CanClose:= true 
end;
Hope you find it useful!

Roo
Delphi Rules!
 
You could turn this into a FAQ as well.

~
Give a man some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
Opieo - I thought about that, but decided to post as a tip first to see if anyone knew a better way or if there was already a standard method of cancelling Message dialogs programically that I may have missed or how my code could be improved. Thanks for your comment!

Roo
Delphi Rules!
 
heres another way.
Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    procedure dothis(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  F: TForm;

implementation

{$R *.DFM}

procedure TForm1.DoThis(Sender: TObject);
begin
 F.Close;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Timer: TTimer;
begin
  F := Dialogs.CreateMessageDialog('HELLOWORLD', dialogs.mtInformation, dialogs.mbOKCancel);
  Timer:=TTimer.create(F);
  Timer.Interval:=5000;
  Timer.OnTimer:=DoThis;
  if F.ShowModal = mrOk then ShowMessage('OK Pressed');
end;

end.

Aaron
 
i found a problem with the above.
the message isnt destroyed so the timer keeps ticking away in the background.

Aaron
 
heres a veriation of the above that works
Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Timer: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure TimerTimer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;
  F: TForm;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
 F := CreateMessageDialog('HELLOWORLD', mtInformation, mbOKCancel);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Timer.Enabled:=true;
  if F.ShowModal = mrOk then ShowMessage('OK Pressed');
end;

procedure TForm1.TimerTimer(Sender: TObject);
begin
  F.Close;
  Timer.Enabled:=False;
end;

end.

Aaron
 
Aaron - Thanks! I've implemented your code into my 'FormCloseQuery'...
Code:
var
  F: TForm;

procedure TManagerMenu.Timer1Timer(Sender: TObject);
begin
  TimesUp:= true;
  Timer1.Enabled:= false;
  if F <> nil then  //make SURE user didn't press a button
    F.Close;  //program hangs if not closed before freed
end;

procedure TManagerMenu.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  F:= CreateMessageDialog('Exit Manager Menu?', mtConfirmation, [mbYes, mbNo]);
  Timer1.Enabled:= true;
  CanClose:= (F.ShowModal = mrYes) or TimesUp;
  FreeAndNil(F);
end;
We still need boolean var TimesUp (initially false) so the timer trips returned var CanClose from FormCloseQuery. I'm not sure this is any better than my PostMessage method, but does demonstrate usage of funtion CreateMessageDialog.



Roo
Delphi Rules!
 
also the bonus with createmessagedialog is that the form F is avaliable to you to put other controls, progress bars ect..

Aaron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top