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

Posted by Daniel - 904 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)

__________________________

Related posts brought to you by Yet Another Related Posts Plugin.

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:





Get my Full Feed Here or you can subscribe to one of my category based feeds below:
Coffee Break

Latest Blog Entries

Categories

Neighbours and Friends

Comments - Thanks Guys :)

  • Therese Lachance: Hi, Any idea how to have ContuttoPDF fetch the correct page language?
  • Marlena Albu: Super Blog, Dude! I am constantly on the watch for new and interesting sites and postings about audio equipment… which is what...
  • tresloukadu: yo how did u fixed when the tags shows <? and it shows < “& l t ; ” ?? please send me an email.
  • Sean: This is a great piece of code and thanks for adding the updates. Sean’s last blog post: Not All Text Message Marketing Is Created...
  • rodhy: Thank for your code, it very usefull for me. best regard.