Back to Chapter 1
Introducing (in this Chapter) :
- Delphis RAD generation: visual objects and events
- Event handlers
- Creating an object (RAD and coding)
- Defining a class
- Declaring an object reference
- Instantiating and freeing an object
Example 1: Extending an empty application
Among the various RAD facilities that Delphi offers is a visual builder for graphical user interfaces (GUIs). This greatly simplifies the building of a user interface and allows the programmer to concentrate on programming. In this example we look briefly at the GUI builder process to write a program with two buttons. Clicking the one turns the form yellow, clicking the other turns the form blue.
Ex 1.2 step 1 A RAD GUI builder
Here we create a simple Delphi program and then look at the RAD generated code in the units listing. Starting with a new application, add two buttons by selecting them from the Component / Tool palette and dropping them onto the form. To change the units name, save it as TwoButtonsU.pas (using the File | Save As menu sequence).Now initialise some properties of the GUI objects in the Object Inspector and notice how they change in the Form Designer. One way to present the properties is by means of a "table":
Component / Property / Value
Form / Name / frmStructureDemo
Form / Caption / Unit Structure
Button1 / Name / btnYellow
Button1 / Caption / &Yellow
Button2 / Name / btnBlue
Button2 / Caption / &Blue
Delphi stores the initial properties in a .dfm file. Representing this properties table in the dfm format gives the following:
object frmStructureDemo: TfrmStructureDemoDelphi 7 shows the structure of the user interface object in the Object TreeView (figure 2). (The Model View in 2006 gives similar functionality.) This gives the containment hierarchy, not the class inheritance hierarchy, and shows that the form object frmStructureDemo contains two buttons, btnBlue and btnYellow. (As we will explore later, Delphi implements this containment through a strong form of aggregation called composition.)
Caption = ' Unit Structure'
object btnYellow: TButton
Caption = '&Yellow'
object btnBlue: TButton
Caption = '&Blue'
Ex 1.2 step 2 Events and their handlers
The GUI interacts with the program code through events. The form itself and each object on the form can give rise to a number of different events in response either to user actions or to system occurrences. One of the more common events is the OnClick event, which is generated when the user clicks with the mouse on a visual object. In this step we write event handlers for each buttons OnClick event.Double-click on btnBlue. Delphis RAD generator inserts an event-handler skeleton in the program code and links the OnClick event to this event handler. Insert the required program statement (line 27 below) and then double-click on btnYellow and insert its program statement (line 23) before saving the unit (File | Save As) as TwoButtonsU and then the project file (File | Save Project As) as TwoButtonsP. Run the program (<F9> or Run | Run). Clicking with the mouse on button Blue makes the form go blue (line 27) while button Yellow makes the form yellow (line 23).
1 unit TwoButtonsU;
2 interface
3 uses
4 Windows, Messages, SysUtils, Variants,
5 Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
6 type
7 TfrmStructureDemo = class(TForm)
8 btnYellow: TButton;
9 btnBlue: TButton;
10 procedure btnYellowClick(Sender: TObject) ;
11 procedure btnBlueClick(Sender: TObject) ;
12 private
13 { Private declarations }
14 public
15 { Public declarations }
16 end;
17 var
18 frmStructureDemo: TfrmStructureDemo;
19 implementation
20 {$R *.dfm}
21 procedure TfrmStructureDemo.btnYellowClick(Sender: TObject) ;
22 begin
23 frmStructureDemo.Color := clYellow;
24 end;
25 procedure TfrmStructureDemo.btnBlueClick(Sender: TObject) ;
26 begin
27 frmStructureDemo.Color := clBlue;
28 end;
29 end.
Ex 1.2 step 3 Analysing the RAD-generated code
Changing the units name
When we saved the unit as TwoButtonsU, Delphi changed the units name from Unit1 to TwoButtonsU (line 1 above). To keep the project tracking synchronised, always change a units name through Save As and dont change the name directly in the program code.
Changing the forms name
In the Object Inspector we changed the Forms name to frmStructureDemo. On this basis, Delphi changed the name of the new form type to TfrmStructureDemo. (It is still derived from TForm, line 7). Delphi then declared the form object as frmStructureDemo (without the T prefix) of type TfrmStructureDemo (with the T prefix) (line 18). (The variable declaration in line 18 means that we can use the name frmStructureDemo to refer to an object of type TfrmStructureDemo.)


