Reference & Resources

OOP in Delphi - TForm objects

When you start a new VCL project, Delphi creates a project file, containing the code for the project:

program Project1;
uses
  Vcl.Forms,
  Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
In the line
Application.CreateForm(TForm1, Form1);
we see how the object Form1 is created automatically. It's created from the class TForm1, which is defined in unit Unit1.pas.
If you add more forms to your project, you'll see additional lines in the project file, like:
Application.CreateForm(TForm2, Form2);
Application.CreateForm(TForm3, Form2);

Delphi also has written the unit for your main form as follows:

unit Unit1;
interface
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils,
  System.Variants, System.Classes, Vcl.Graphics, 
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
end.

In the implementation section of Unit1, note this line:
{$R *.dfm}
It means that additional info for TForm1 is in an additional file Unit1.dfm, that contains some of its properties.
Let's have a look at the contents of the dfm-file: in the IDE, switch to the Design view, right-click on Form1 and select "View as text". You'll see something like this (some details depend on the settings of your Delphi IDE):

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 299
  ClientWidth = 635
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
end
Later on, if you define event handlers for the form, they will also be listed here. As for now, we don't have any event handlers.


Changing the form's properties

Let's change a few properties (with the Object Inspector):
Set property Hint to This is the main form
Set property ShowHint to True

Check again with "View as text": now, there will be two additional lines:
Hint = 'This is the main form'
ShowHint = True

Save your files and test the application.


Adding components to the form

Let's add components to our main form, for example: a TEdit, a TButton and a TLabel.

Write an event-handler for Button1: double click on Button1 and next complete the Delphi generated code:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := Edit1.Text;
end;
Save your files and test.

Let's say we want the edit-box to be empty at the start of the program. Of course, we could empty its property Text in the Object Inspector; also in Design-view the text would disappear, but we want to keep it as to remind us of the name of the TEdit component. So, let's clear Edit1 right after it is created.

  1. In the Object Inspector, select Form1.
  2. Switch to the Events and double click next to OnCreate.
  3. In the code view, complete the newly created event handler:
procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.Text := '';
end;

Have a look at the changes in the type declaration in Unit1.pas and at the text-view of Unit1.dfm and you'll realize how much time Delphi saved you by being "RAD" and "OOP" :)


[ Will be continued soon ]

OOP in Delphi
Table of contents

1. Object - Class
2. TForm objects
3. Hierarchy of classes

 
Reference  Crash Course Delphi  FAQ
Tips  Source Code  Downloads  Links

Copyright 1999-2018 
DelphiLand