How to close a modal form after opening it?

advertisements

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?


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;