Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I close a modal form right after opening it?

Tags:

dialog

delphi

From my application I wish to open a dialog, which should close immediately (after a short message) under some circumstances.

I've tried this:

procedure TForm2.FormActivate(Sender: TObject);
begin
  if SomeCondition then
  begin
    ShowMessage('You can''t use this dialog right now.');
    close;
    modalresult := mrCancel;
  end;
end;

but the dialog remains open. I've also tried to put the code in the OnShow event, but the result is the same.

Why doesn't this work?

like image 588
Svein Bringsli Avatar asked Aug 06 '10 08:08

Svein Bringsli


3 Answers

Post a WM_CLOSE message instead of calling close directly;

ShowMessage('You can''t use this dialog right now.');
PostMessage(Handle, WM_CLOSE, 0, 0);
modalresult := mrCancel;
like image 152
Sertac Akyuz Avatar answered Nov 12 '22 07:11

Sertac Akyuz


try this one

procedure TForm2.FormActivate(Sender: TObject);
begin
  ShowMessage('You can''t use this dialog right now.');
  PostMessage(Self.Handle,wm_close,0,0);
end;
like image 20
Bharat Avatar answered Nov 12 '22 07:11

Bharat


Wouldn't it be easier to check the certain circumstance before the form opens, and not open it?

I can't see a reason for the form to stay open, it should disappear immediatly after clicking OK on the show message dialog.

The showmessage is blocking so you won't be able to close until that's OK'd (if you need to close before then you could return a different modal result (or make your own up which doesn't clash with the existing ones like mrUnavailable = 12). Then you could show the message if the ModalResult was mrunavailable.

If it is running the code and just not closing then try using Release instead of close.

Edit: if you re-using the same form in several places don't use Release unless you want to recreate the form every time! Post the close message as the others have suggested

like image 37
James Barrass Avatar answered Nov 12 '22 09:11

James Barrass