Question

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?

Was it helpful?

Solution 3

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.

OTHER TIPS

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.

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.

Before closing the 1st modal form I would post a custom message to the application's mainform (or other), which would cause it to show the 2nd modal form. I prefer the idea of the 'owner' of both forms being in control.

PostMessage(Application.Mainform.Handle, DO_OPEN_2ND_MODALFORM, 0, 0);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top