1. Home
  2. Computing & Technology
  3. Delphi Programming

A Second Form in a Program

By , About.com Guide

Ex 1.3 step 2 An Auxiliary Form

Add a second form to the project (File | New | Form in Delphi 7, File | New | VCL Form in Delphi 2006). This will consist of a blank form, TfrmAuxiliary, with no other components (figure 8 & figure 9). It inherits the ability to Show and Hide itself from TForm, the library class from which all forms are derived (line 7 below). We use the OnShow event handler (lines 14 to 18) to define TfrmAuxiliary’s additional behaviour: it sets the position on the screen randomly each time the form is shown (lines 16 & 17).

The random number generator needs to be seeded and we choose to seed it in the initialization section (lines 19–20): we could also have placed the Randomize statement in the Form’s OnCreate event handler. In any unit the initialization section is optional.

If present, the statements in this section run when the program starts, before any of the other program statements in that unit. It begins with the reserved word initialization and continues until the beginning of the finalization section or, if there is no finalization section, until the end of the unit. If there are several units in a project, the different initialization sections run in the order that the units appear in the project file. If a unit uses any other units, their initialization sections run before its does, with the units in the interface section’s uses clause running before those in the implementation section.

1 unit AuxForm;

2 interface

3 uses
4 Windows, Messages, SysUtils, Variants, Classes,
5 Graphics, Controls, Forms, Dialogs, StdCtrls;

6 type
7 TfrmAuxiliary = class(TForm)
8 procedure FormShow(Sender: TObject) ;
9 end

10 var
11 frmAuxiliary: TfrmAuxiliary;

12 implementation

13 {$R *.dfm}

14 procedure TfrmAuxiliary.FormShow(Sender: TObject) ;
15 begin
16 Left := Random (600) ;
17 Top := Random (400) ;
18 end;

19 initialization
20 Randomize;

21 end.
(The screen positions and references in lines 16 & 17 refer to a low resolution screen. If you are using a higher resolution display you may want to increase these values proportionately.)

Ex 1.3 step 3 The Project File

Delphi keeps track of the different units in a program through the Project file. To see the project file for this example, use Program | View Source.
program TwoUnits;

uses
   Forms,
   MainForm in 'MainForm.pas' {frmMain},
   AuxForm in 'AuxForm.pas' {frmAuxiliary};

{$R *.res}

begin
   Application.Initialize;
   Application.CreateForm(TfrmMain, frmMain) ;
   Application.CreateForm(TfrmAuxiliary, frmAuxiliary) ;
   Application.Run;
end.

Ex 1.3 step 7 Explicit Create and Free

When we start a new application or add another form to an existing application, Delphi automatically, as part of its RAD capability, sets up the class definition of the form (eg lines 7–16 in step 1 (unit MainForm) and lines 7–9 in step 2 (unit AuxForm)), declares a reference to an object of this class (eg lines 17–18 in step 1 and lines 10–11 in step 2) and then constructs the object (eg lines 9–10 in step 3 (the project file program TwoUnits)).

So, with the way Delphi (and several other languages) implements OO, there are three phases to creating an object:

  • Define the class
  • Declare a reference to the object (or instance), and
  • Create the object
The class definition is a set of instructions for creating objects, a bit like a set of manufacturing plans. The object declaration sets up a mechanism for accessing the object by creating a reference on the stack. The object constructor uses the class definition to create an object in memory (the heap).

In the present version of the program Delphi takes responsibility for all these steps. Quite soon we’ll define classes and declare and create objects in the programs we write. As a first step towards this, we’ll intervene in the auto-create process for RAD objects and create the auxiliary form as part of a program instead of allowing Delphi to create it automatically. We can remove any of the forms in a project except the main form from the auto-create list. If we do this, Delphi will no longer auto-create those forms and the programmer must create them explicitly in program code before they can be used. (We can also choose which form will be the main form.)

To avoid overwriting the current version of this code, save these two units and the project file to a new directory for this example before doing anything else.

Now use the Project | Options menu sequence and select the Forms options tab. The left pane lists the auto-create forms. Select frmAuxiliary in this pane and click the single right arrow to transfer it from the auto-create list to the available forms list. Select OK to close the Project Options window.

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. OOP in Delphi
  6. Free Online OOP Course
  7. Learning Object Oriented Programming with Delphi: Chapter 2

©2009 About.com, a part of The New York Times Company.

All rights reserved.