Chapter 13 Dynamic Objects
Chapter 13 Dynamic Objects
Introduction
In some applications, programmers have to use many components on the interface. (Think for
example about all the images which are used in games such as solitaire.) Every component takes up
space in memory of the computer. It also occupies space on the Form. Therefore programming
languages provide the option for programmers to also create a component when the program is
already running. This can be very useful, for example when games applications are developed, where
images can be created as the game progresses, and destroyed again when there is no longer any use
for the these images. When a component is created during run-time of an application, it is called
dynamic instantiation of an object.
When the program starts, only on Button [Login] is visible. When the user clicks on the Button, the
[Login] Button disappears, and a Panel containing a greeting, as well as a [Start] Button appear.
Once the user has read the message, the [Start] Button can be clicked to display the actual interface
of the program. When the [Start] Button is clicked, the Panel containing the greeting should
disappear. In the explanation of the program which follows, we will show you how the Panel can be
created and destroyed again while the programming is executing (dynamically instantiated). (The
Buttons are just displayed or hidden using the Visible property).
1
Task Object Event
1. Make btnLogin visible frmCashRegister OnShow
2. Make btnStart invisible
1. Make btnLogin invisible btnLogin Click
2. Make btnStart visible
3. Dynamically instantiate pnlGreet
4. Set all the properties of pnlGreet
1. Make btnStart invisible btnStart Click
2. Destroy pnlGreet
3. Make the components of the program visible
type
TfrmCashRegister = class(TForm)
These are the names for each
btnStart: TButton;
component the programmer places on
btnLogin: TButton; the Form. Delphi automatically creates
procedure btnStartClick(Sender: TObject); these variables.
procedure btnLoginClick(Sender: TObject);
procedure FormShow(Sender: TObject);
private
The programmer declares a variable
pnlGreet : TPanel ; (pnlGreet). This variable will be used to
public refer to the Panel object which will be
end; instantiated dynamically. It is declared as
var a variable with class scope, because two
different event handlers will need to have
frmCashRegister: TfrmCashRegister;
access to this component (the Panel).
implementation
{$R *.dfm}
end;
The Panel is created (instantiated) by
calling the Create method of the TPanel
procedure [Link](Sender: TObject);
class. The value in brackets indicates that
begin
the Form called frmCashRegister must be
[Link] := False ; the owner of the Panel.
[Link] := True ;
pnlGreet := [Link](frmCashRegister);
Note:
x A component must have an owner and a parent. Delphi takes care of this automatically when
you choose a component from the Palette and drop it on the Form. With a dynamic component
you have to indicate it specifically.
x The owner of a component controls the existence of the component. When the owner is
destroyed, all the components it owns are also destroyed. You can also destroy a dynamic
component explicitly before its owner is destroyed.
x The parent of a component controls the display of the component. The parent makes the
component visible, and it controls the format (the way the child component is displayed). For
example:
o Certain properties of the parent can be inherited by the child automatically e.g. the
Font.
o When the Left and Top properties of a child component is set, it is relative to the parent
component. For example if an Image is the child of a Panel, and the Left property of the
Image is set to 50, it will be 50 pixels from the side of the Panel, not from the side of the
Form.
o (If you do not set the Parent property, the component will not be visible.)
x You can also use the Destroy method to free the memory occupied by a dynamic object,
however, it is safer to use the Free method because:
o If you use Destroy on an object that does not exist anymore, you will receive an Access
violation error. The Free method will always test if an object does exist, only if it does
exist the object will be destroyed.
3
Activity 1: Create a dynamic Image
x Open the program frmCashRegister_p.dpr which has been supplied as data for the chapter.
Add code to create a dynamic image which will be instantiated and destroyed at the same
time as the greeting.
o The image you want to load onto the Image component must be stored in the same
folder as the program.
o The following is an example of an instruction to load a picture into the dynamically
instantiated Image component (the names you choose for the Image and the file
may be different).
[Link]('[Link]') ;
If you want to use a file of another format such as .jpg, you have to tell Delphi where to find the
code needed to decode the format. You do this by adding the name of the library (unit) where the
code is stored to the uses clause at the beginning of the Form’s Unit.
unit frmCashRegister_u;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls,
StdCtrls, jpeg;
4
These two components are LabeledEdits (of
type TLabeledEdit). They will be created in the
When Button [Next customer] is clicked, the
event handler of the OnClick event of the
following should happen:
CheckBox.
x The memory occupied by each
component (the LabeledEdits) should be released using the Free method.
x The variables referring to the components should be cleared.
When the user clicks on Button [Buy], the event handler should read the values from the
LabeledEdits, and calculate the final cost. However, the program should not try to read a value from
the dynamic components if they do not exist. For this example, there are two ways to ensure the
LabeledEdits will only be accessed if they do exist:
iNumAdults := StrToInt([Link]) ;
iNumChildren := StrToInt([Link]) ;
end
Method 2:
An assigned function can be used to determine if one of
if assigned(edtNumAdults) then
the LabeledEdits exists.
begin
iNumAdults := StrToInt([Link]) ;
iNumChildren := StrToInt([Link]) ;
end
5
Activity 2: Create a dynamic component - use it more than once in the
program
Write a Delphi program for the previous example making use The interface for activity 2 is
of dynamic components. The total cost should be displayed
available with the data for the
when the user clicks on Button [Buy]. The cost of one ticket
chapter.
for the show is R50.00, a dinner ticket for an adult costs
R100.00, and a dinner ticket for a child costs R25.00.
x If a user clicks the CheckBox the dynamic components (LabeledEdits) should appear. But if
the user changes his mind immediately, and ‘unticks’ the CheckBox, the LabeledEdits should
diappear again.
x When the [Next customer] Button is clicked, all the components should be cleared, and the
focus placed in the SpinEdit (number of tickets).
child of MyImage
frmPictures MyPanel
child of
MyLabel
child of
You can open the file frmPicture_p.dpr provided as data for this chapter to review this program.
6
type
TfrmPicture = class(TForm)
btnShow: TButton;
btnDestroy: TButton;
procedure btnShowClick(Sender: TObject);
procedure btnDestroyClick(Sender: TObject);
private
{ Private declarations }
MyPanel: TPanel;
MyImageOnPanel: TImage;
MyLabel: TLabel;
public
{ Public declarations }
end;
var
frmPicture: TfrmPicture;
implementation
{$R *.dfm}
with MyPanel do
begin
Parent := frmPicture;
Left := 20; The Left and Top properties are
Top := 100; relative to the parent (frmPicture)
Width := 350;
Height := 350;
Caption := '';
TabOrder := 0;
Color := clBlack;
end;
with MyImageOnPanel do
begin
Parent := MyPanel;
Left := 50;
Top := 50;
Width := 250;
Height := 260; MyPanel is the parent of the
[Link]('[Link]');
Stretch := True; components MyImageOnPanel and
end; MyLabel. Therefore these Left and
with MyLabel do Top properties are relative to
begin
Parent := MyPanel; MyPanel.
Left := 120;
Top := 10;
Width := 30;
Height := 20;
Caption := 'The flower';
[Link] := [fsBold, fsUnderline];
[Link] := clWhite;
end;
end;
7
procedure [Link](Sender: TObject);
begin
[Link];
MyPanel := nil ;
end;
end.
x MyPanel is a child object created on frmPicture.
x Both MyImage and MyLabel are child objects of the MyPanel component. Therefore when
you destroy the MyPanel component you will destroy both child objects as well. Since all of
the components are child objects of frmPicture they will all be destroyed when frmPicture is
closed.
1. A file [Link] contains 30 ID numbers. Write a program that will read these numbers from the
text file into an array of strings. The interface of the program should have two Buttons.
2. Create a Delphi program to play the game Rock – Scissors – Paper. The interface of the
program is provided as data for the chapter. When the user clicks on the Button [Show the
rules of the game], a Panel with
other components should be created
dynamically to display the rules of
the game. When the user clicks on
any Button, Image, RadioButton or
the Form, the Panel and its children
should disappear.
You need to write the declaration for the event handler of the dynamic Button yourself. (The
definitions of the other components’ event handlers are created by Delphi automatically.) You can
choose any name for the event handler. We place the event handler in the private section of the
class so it is clear that it was created by the programmer. You can also place it with the other event
handlers above the private section. An event handler needs a parameter of type TObject.
When you press <Ctrl> <Shift> <c>, Delphi will provide a framework for the event handler, and you
can then complete the code.
The executable file for the previous example is provided as The interface for activity 4 is
data for this chapter. Run the program, and then create a available with the data for the
Delphi program to duplicate the program. chapter.
The property Strings contains the strings itself and works similar to the Items property of a ListBox or
RadioGroup.
x Use the LoadFromFile method to load data from the text file [Link] into the
TStringList object.
x Both the class TStringList and the RichEdit has a Text property. You can assign the Text
property of the StringList to the Text property of the RichEdit to transfer all the strings to the
RichEdit in one statement.
The interface for activity 5 is
[Sort the list of strings]: available with the data for the
x Use the Sort method. chapter.
x Use the Exchange method. You can randomly exchange a number of lines with one another.
x Use the SaveToFile method to save the content of the TStringList back to the text file
[Link] .
11
uses
{existing uses},ComObj; Add the ComObj unit to the uses-clause.
If you are using a windows version older than Vista the lines shaded in grey are not needed.
12