Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi return custom result for showmodal

I have a form with 2 buttons (1 is mrOK - 1 is mrCancel). As soon as I click one of the buttons the form closes (OnClose gets called), no matter what.

I would like to return a custom value. like this:

procedure OpenForm;
var
 MyForm : TMyForm;
begin
 MyForm := TMyForm.Create (NIL);
 try 
  if MyForm.ShowModal = 1337 then begin
   // [...]
  end;
 finally
  MyForm.Free
 end;
end;

The Modal Form:

 procedure TMyForm.Button1Click(Sender: TObject); // mrOK
 begin
  if Edit1.Text = '' then abort; // Don't close here?!
 end;

 procedure TExecutePrompt.FormClose(Sender: TObject;
 var Action: TCloseAction);
 begin
  if Edit1.Text = '' then abort; // Works but if the user clicks the X it should return mrCancel
 end;

Hope you understand what I want to do. it's a prompt window with a edit control. if theres no text in the control the form should stay until text is entered (unless the X is clicked).

Thanks for your help.

like image 918
Ben Avatar asked Sep 15 '25 19:09

Ben


1 Answers

To close a modal window with some particular modal result value, simply assign

ModalResult := MyVal; // This will close this modal window
                      // and the modal result will be MyVal

That is, make sure that Button1 has ModalResult = mrNone, and then you can do things like

procedure TMyForm.Button1Click(Sender: TObject); // mrOK
begin
  if Edit1.Text <> '' then ModalResult := 1337;
end;

This will close the form if the edit box isn't empty, and the modal result will be 1337.

like image 133
Andreas Rejbrand Avatar answered Sep 17 '25 20:09

Andreas Rejbrand