• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1809
  • Last Modified:

Delphi Correct way to create and free mdi child forms

Good day

What is the correct way to create a mdi child form from a menu and to destroy that form.
All my forms in my applications are not auto created.

Thank you
1 Solution
If TForm1 is the MDI parent, and TForm2 is the MDI child, then you add the childs by just creating an instance of the child form with the owner form as parent. And to free the form, you assign the FormClose event on TFrame2, and set CloseAction to caFree when the event fires. This will cause the Form to be freed when the user closes the form. (Don't forget to set the FormStyle property on both TForms)

See examples below:


{$R *.dfm}
uses Unit2; // <- In Unit1, you add the child MDI unit below the implementation line so that you don't get circular references.  
// In your menu, you create a child form class and set Self as Owner...
procedure TForm1.NewChild1Click(Sender: TObject);
// In your MDI child's OnClose
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
  Action := caFree; // <- causes the form to be freed... 

Open in new window

henryreynoldsAuthor Commented:
Hi Hypo

Thank you.

Keep well
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now