Delphi return custom result for showmodal
16,606
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
.
Author by
Ben
Updated on June 05, 2022Comments
-
Ben almost 2 years
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.
-
Ben over 11 yearsThat was it thank you. It also has to be mrNone! This could have been a bad trap.
-
TLama over 11 yearsJust to be more precise to close a modal window assign a value different from 0.
-
David Heffernan over 11 yearsRather than writing an OnClick handler you can simply set the ModalResult property in the form designer to be your special value.
-
Andreas Rejbrand over 11 years@David: But then you will lose some freedom.
-
Andreas Rejbrand over 11 years@David: This kind of freedom:
if Sth1 then ModalResult := 1337 else if Sth2 then ModalResult := 1338