Runtime-Form-Creation. Automatically creating child forms in a Delphi MDI application with a component array

Posted by Daniel - 1,384 Views

delphi tips,runtime form creation, mdi applicationThis article will give you an example of runtime-form-creation, or how to create child forms in a Delphi MDI application using the component array. It means that the forms will be created on runtime. You can create and remove forms not in the design mode but when the program is running.

The program consists of an MDIForm, there is a Main Menu on the form and the screenshot is below:

mdi.gifUnder the [File] menu there is a menu item [New Window]. If you click on that item, a new child form will be created and the reference to it will be added under the [Windows] menu. If you close the just created form, the instance of the form will be removed and its reference under the [Windows] menu will be deleted.

You can also change the TileMode of the created child forms by choosing on of the available modes: [Tile Horizontally] or [Tile Vertically] from the [Layout] menu. The form creation is done at runtime by the procedure below:

procedure TForm1.mmNewWindowClick(Sender: TObject);
var Daftar : TMenuItem;
i : integer;
boolCaptionExist : boolean;
begin
SetLength(arrForm,Length(arrForm)+1);
arrForm[Length(arrForm)-1] := TForm.Create(Form1);
arrForm[Length(arrForm)-1].FormStyle := fsMDIChild;
arrForm[Length(arrForm)-1].OnClose := FormChildClose;

boolCaptionExist := false;

for i := Low(arrForm) to High(arrForm) do
begin
if arrForm[i].Caption = ‘Window ‘ + IntToStr(Length(arrForm)) then
begin
boolCaptionExist := True;
end;
end;

if boolCaptionExist then
arrForm[Length(arrForm)-1].Caption := ‘Window ‘ + IntToStr(Length(arrForm)+1)
else
arrForm[Length(arrForm)-1].Caption := ‘Window ‘ + IntToStr(Length(arrForm));

arrForm[Length(arrForm)-1].Show;

Daftar := TMenuItem.Create(mmWindows);
Daftar.Caption := ‘Window ‘ + IntToStr(Length(arrForm));
Daftar.OnClick := WindowListClick;
mmWindows.Insert(mmWindows.Count,Daftar);
end;

There is an array altering process performed by the SetLength command. arrForm() is a dynamic array where the child form instances stored. The datatype of the array is TForm because we want to store forms in the array, and the declaration which you can also find it at the top of the full code is below:

arrForm: array of TForm;

Everytime a child form is created, the length of the array will be added by 1 and vice versa. The full example project code is included in this artcle, just download it if you want to learn more about runtime-form-creation.

Download the example project (9 KB)

share this article

Digg del.icio.us Netscape StumbleUpon Yahoo! MyWeb reddit Furl Magnolia Newsvine Technorati SlashDot Blinklist Simpy Google
This post as PDFPosted in: Programming - January 2008


Leave a Reply


Options for your comment:





Hi, my name is Daniel Nugraha, a single male live on an island called Java, Indonesia. This is the place for me to share my interest in computer programming.

Coffee Break

Comments - Thanks Guys :)

  • Rangga Kusuma: Gan, Tengkiu buat postingan yang sangat berguna. Kebetulan ada project utk bikin sms gateway, dan converter Agan sangat berguna utk...
  • Chuck Norton: I actually went ahead & bolted over to Justin’s Get The Image plugin here: http://justintadlock.com/ar...
  • Chuck Norton: Question: is it possible to insert something like [custfieldimg=”joice1.jpg,15 0,1:1″] into the actual templates instead of...
  • Therese Lachance: Hi, Any idea how to have ContuttoPDF fetch the correct page language?
  • tresloukadu: yo how did u fixed when the tags shows <? and it shows < “& l t ; ” ?? please send me an email.