Runtime-Form-Creation. Automatically creating child forms in a Delphi MDI application with a component array
Posted by Daniel - 21,310 Views
This 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:
Under 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:
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:
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)
The following posts are programmatically considered as related to the current post by YARPP Plugin:
- CPort Component (Serial port interface component for Delphi)
- MPEG Audio Component (A Delphi component to play audio files)
- FlatStyle Delphi controls (Flat controls for your Delphi application)
- Delphi SMSCodec (SMS Processing Component)
- An example: Using CPort Delphi Component to read data from your cellphone
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.
-
Get my Full Feed Here
Popular Entries
- Passing arguments to your VB.NET console application
- Microsoft Excel Import External Data Problem: When Microsoft Query doesn’t recognize some of your parameters
- Resize Image or Crop Image with Joe Lencioni’s Smart Image Resizer, WordPress Setup
- How to Get User Input and allowing more than 256 characters to be entered on .NET Console Application
- ASCII To PDU Converter (Convert ASCII to PDU and vice versa)
- ConsoleProgressBar - Simple Progress Bar Function for your VB.Net Console Application
- An example: Using CPort Delphi Component to read data from your cellphone
- Runtime-Form-Creation. Automatically creating child forms in a Delphi MDI application with a component array
- CPort Component (Serial port interface component for Delphi)
- SmartImageResizer Plugin, WordPress plugin based on Joe Lencioni’s Smart Image Resizer













