Working with MDI Forms in Delphi

Soon Sam Santos
4 min readDec 9, 2018

MDI Form is great if you want to work with a MainForm and open ChildForms thorughout the app usage.

Note that we have Parent Form and then a Child Form inside the Parent one.

MDI Forms is also very useful if you are using MainMenu as navigation in your app.

In this tutorial I’ll be using Delphi 10.3 Community Edition.

How to create a MDI Form?

  1. Create a new project in Delphi.
  2. Instead of calling this first created form of Form1, let’s call it frmParent/uParent.
  3. In the Object Inspector select your Form and change the property FormStyle to fsMDIForm.

4. Now it’s time to create your Child Form. Create a new VCL Form and name it frmChild/uChild.

5. As you did in step 3 above, change your frmChild FormStyle property now to fsMDIChild.

Now, your MDI Forms are all set, you have a Parent Form (Main) and a Child Form (Child). We just have to do a couple of adjustments.

Adjustments

  1. We want our forms to open in full screen mode. To do that, select your frmParent, go to the Object Inspector and change WindowState property to wsMaximized. Do the samething for your Child Form (frmChild).

Open Child from Parent

You may notice that when you run your project now, both forms will be created. You need to delete frmChild from the auto-created forms so it’ll be created only when we make some action in frmParent.

In the delphi toolbar go to → Project → View Source

You will see the following code.

program MDIForms;uses
Vcl.Forms,
uParent in 'uParent.pas' {frmParent},
uChild in 'uChild.pas' {frmChild};
{$R *.res}begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TfrmParent, frmParent);
// Application.CreateForm(TfrmChild, frmChild);
Application.Run;
end.

From this code you can see that frmParent and frmChild are being auto-created. Delete or comment out the line which creates the frmChild. So it’ll be not created anymore when you start your program.

After that, you must include a button in the Parent Form that will be responsible to start Child Form.

  1. From the Pallete drop a TMainMenu in your frmParent.
  2. Click twice in the MainMenu and a window will popup. In the first little box you can click once and type the Caption you want that button to have. Let’s type Open Child Form.
  3. Now Click twice in Open Child Form to open the Code tab.

Then type the following code.

uses
uChild; // Include this line to access Child Unit from this Parent // Unit.
{$R *.dfm}procedure TfrmParent.OpenChildForm1Click(Sender: TObject);
begin
if not Assigned(frmChild) then
begin
frmChild := TFrmChild.Create(Application);
frmChild.Show;
end
else
frmChild.WindowState := wsMaximized;
end;

First, we check if frmChild is not assigned yet, if so, we create a frmChild instance and Show it. Otherwise, we just maximize the screen.

Now, you will see that when you click in the button the Child Form will be created, however, it will be not closed. That’s why you have to do the next step.

4. Open your frmChild and include the FormClose event to your Unit.

In uChild.pas

procedure TfrmChild.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
Release;
frmChild := nil;
end;

With this code set, you’ll notice now that when you open your application only the parent form is auto-created, then if you click in the button the child form is created and you can close the child form whenever you want.

Adjustments

When droping some widgets to your Child Form you will notice that it may not fit the full screen, eventhough it is maximized.

To fix that you must drop a TPanel in your Child Form, change its width and height to ocuppy the full screen and set its alignment property to alClient in the Object Inspector.

To set Align to alClient just click in the middle big box.

After that, you can drop your widgets inside your panel so they will also fit the entire screen.

Remember that for some widgets you need to set Anchor property Top,Left,Right and Bottom to True so they can follow along with the panel size.

--

--

Soon Sam Santos

Flutter and Delphi Developer at Green. Teacher at SoonClass