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

Learning Object Oriented Programming with Delphi: Chapter 3
Example 1.4: Defining a Form Directly in Code

By , About.com Guide

In the previous example, we used Delphi’s RAD facility to define the class TfrmAuxiliary. We then instantiated and freed it in program code. As an alternative, in this example we define TfrmAuxiliary directly from TForm using program code and then instantiate it.

Ex 1.4 step 1 Modifying the previous program

In a spirit of reuse, and to emphasise the changes we’ll be making in this example, open the project from the previous example and then save it (ie the two units and the project file) to a new directory. We’ll modify this copy. Since we’ll define the auxiliary form directly from TForm (line 48 below) we don’t need unit AuxForm of the previous example. So in unit MainForm delete the local uses AuxForm statement. Now we need to remove AuxForm from the project. Use the menu sequence Project | Remove from Project, select unit AuxForm and click on OK. (To keep your directory tidy, you could now also delete unit AuxForm from the directory you are using for this example.)

Unit AuxForm declared a reference to frmAuxiliary. This unit is no longer part of the project and so we declare this reference as a private data field in the type declaration of TfrmMain (lines 17–18 below). The reference frmAuxiliary is now a private data field in TfrmMain.

As in the previous example, if the object that frmAuxiliary refers to does not already exist (line 46) we create it, but this time we create it directly from the library class TForm (line 48). (In the previous example we created it from TfrmAuxiliary: example 1.3, step 4, line 44.) We now initialise it in program code (lines 49–51) since we no longer have access to the Object Inspector to initialise frmAuxiliary. We also set the values of Left and Top for frmAuxiliary (lines 52–53) since we don’t have a suitable OnShow event handler (cf example 1.3, step 2, lines 18–22). Showing, Hiding and Freeing frmAuxiliary remains the same process as before.

unit MainForm;

interface
3
4   uses
5     Windows, Messages, SysUtils, Variants, Classes, Graphics,
6     Controls, Forms, Dialogs, StdCtrls;
type
8   TfrmMain = class(TForm)
9    radAuxShow: TRadioButton;
10    radAuxHide: TRadioButton;
11    gpbAuxiliary: TGroupBox;
12    btnCreate: TButton;
13    btnFree: TButton;
14    procedure radAuxShowClick(Sender: TObject) ;
15    procedure radAuxHideClick(Sender: TObject) ;
16    procedure btnCreateClick(Sender: TObject) ;
17    procedure btnFreeClick(Sender: TObject) ;
18  private
19    frmAuxiliary: TForm; // Composition
20  end;

21  var
22    frmMain: TfrmMain;

23  implementation
24  {$R *.dfm}

25  procedure TfrmMain.radAuxShowClick(Sender: TObject) ;
26  begin
27   try
28    frmAuxiliary.Show;
29   except
30    ShowMessage ('Auxiliary Form does not exist') ;
31    radAuxShow.Checked := False;
32    radAuxHide.Checked := False;
33   end;
34  end;

35  procedure TfrmMain.radAuxHideClick(Sender: TObject) ;
36  begin
37   try
38    frmAuxiliary.Hide;
39   except
40    ShowMessage ('Auxiliary Form does not exist') ;
41    radAuxHide.Checked := False;
42    radAuxShow.Checked := False;
43   end;
44  end;

45  procedure TfrmMain.btnCreateClick(Sender: TObject) ;
46  begin
47   if frmAuxiliary = nil then
48   begin
49    frmAuxiliary := TForm.Create(nil) ;
50    frmAuxiliary.Caption := 'frmAuxiliary';
51    frmAuxiliary.Height := 150;
52    frmAuxiliary.Width := 180;
53    frmAuxiliary.Left := Random (600) ;
54    frmAuxiliary.Top := Random (400) ;
55   end
56   else
57    ShowMessage ('Auxiliary Form already exists') ;
58  end;
When you run this program it appears on the screen much as the previous examples. So what is the significance of the changes we have made?

We declare frmAuxiliary (of class TForm) as a constituent of class TfrmMain by declaring it as a private data member (or data field) (line 18). We then create it (line 48), manipulate it (lines 49–53) and, when we have finished with it, we free it (line 62). This private create-use-free relationship between one object and another is an example of composition. It’s similar to association, but where association is a relationship between two independent objects, with composition one object creates, uses and destroys a private subsidiary object.

Composition is an important concept in OO and we’ll return to it several times in the chapters that follow.

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 3

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

All rights reserved.