DELPHI. How to close the modal form just after calling another modal form

13,918

Solution 1

I Hide the FormA before showing modal FormB. Then depending on ModalResult of FormB show or close the formA.

Hide; 
FormB.ShowModal; 
if FormB.ModalResult <> mrOK then Close; 

ModalResult = mrOK means that Formb has opened a MDIchild form and was closed.

Solution 2

Modality implies lifetime nesting. When one modal form opens another modal form, the first form needs to remain during the entire lifetime of the second modal form.

So, what you need to do is close the first modal form before you show the second modal form. That's a little tricky to do from inside the first modal form so it may be best to ask the main form for help. The main form can:

  1. Call Free on the first modal form.
  2. Create and show the second modal form.

If the first modal form needs to trigger this from one of its own event handlers, then the best way forward is for the first modal form to queue a message to the main form. For instance with PostMessage or TThread.Queue.

Solution 3

Setting a modally shown form's ModalResult property to a value other than mrNone will cause the form to be closed.

procedure TFormA.Button1Click(Sender: TObject);
begin
  ShowFormBModal;
  ModalResult := mrCancel; // this will close Form A if it's being shown modally
end;

Depending on your requirements, the actual value of ModalResult may depend on the modal result of Form B or other conditions.

Share:
13,918
Avrob
Author by

Avrob

Updated on June 29, 2022

Comments

  • Avrob
    Avrob almost 2 years

    IN MDI application there is an opened modal form A. The form B is being shown modal from form A. How can I close the modal form A just after calling modal form B?