Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 36 ยท Forms and Tooling

VCL Forms and Components

Understand how Delphi builds windows from forms and components, how Owner and Parent differ, how .dfm files and the Object Inspector work, and how to create controls in code.

Delphi became famous for rapid application development: drag a button onto a window, double-click it, write one line of code, and press F9. Underneath that speed is a very consistent object model. Every window, button and timer is an object of a class, and you can create all of them in code instead of the designer.

This lesson explains that model: what a form is, what a component is, the difference between Owner and Parent (a common source of confusion), and how the form designer files work. It applies to the VCL (Visual Component Library, Windows) and, with different class names, to FireMonkey (FMX, cross-platform).

The IDE and the VCL cannot run in the browser, so the Pascal code is read-only. The runnable program models the ownership tree in C.

Components, controls, forms

The class hierarchy gives each level a purpose:

flowchart TD
  O["TObject"] --> P["TPersistent"]
  P --> C["TComponent<br/>(can live on a form, has Owner, Name)"]
  C --> W["TControl<br/>(visible, has Parent, position)"]
  W --> WC["TWinControl<br/>(has a window handle, can focus)"]
  WC --> F["TCustomForm / TForm"]
  WC --> B["TButton, TEdit, TPanel..."]
  C --> N["TTimer, TOpenDialog, TFDQuery<br/>(non-visual components)"]

Visible controls are components that also know how to draw themselves.

  • A component (TComponent) is anything you can place in the designer and configure in the Object Inspector. Non-visual ones, such as TTimer or a database connection, show as little icons on the form.
  • A control (TControl) is a component with a screen position and size.
  • A form (TForm) is the top-level window. Your own form is a class that descends from it: TMainForm = class(TForm).

A form in code

When you add a button in the designer, the IDE changes two files. The .pas unit gets a field and an event handler:

PASCAL
type
  TMainForm = class(TForm)
    GreetButton: TButton;
    NameEdit: TEdit;
    procedure GreetButtonClick(Sender: TObject);
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

procedure TMainForm.GreetButtonClick(Sender: TObject);
begin
  ShowMessage('Hello, ' + NameEdit.Text + '!');
end;

The {$R *.dfm} line links the form description file into the program. The fields are declared at the top of the class without a visibility label; that special section, managed by the IDE, is where the designer puts them. Do not edit it by hand unless you know what you are doing.

The .dfm file

The second file, MainUnit.dfm, describes the form's settings as text:

TEXT
object MainForm: TMainForm
  Left = 0
  Top = 0
  Caption = 'Greeter'
  ClientHeight = 120
  ClientWidth = 280
  object NameEdit: TEdit
    Left = 16
    Top = 16
    Width = 249
    Height = 23
    TabOrder = 0
  end
  object GreetButton: TButton
    Left = 16
    Top = 56
    Width = 75
    Height = 25
    Caption = 'Greet'
    TabOrder = 1
    OnClick = GreetButtonClick
  end
end

When the program starts, the form is created and its properties are streamed in from this resource: each object becomes a component, each line sets a property, and OnClick = GreetButtonClick connects an event to a method with that name. This is why only published properties appear in the Object Inspector (see properties and visibility). Press Alt+F12 in the IDE to switch between the form view and this text.

Owner versus Parent

Two relationships look similar but do different jobs:

Owner Parent
Set by TComponent.Create(AOwner) the Parent property
Meaning who frees me where I am drawn
Applies to every component visible controls
Example the form owns every button a panel is the parent of the buttons inside it

When you free a form, it frees every component it owns. A button inside a panel is drawn inside the panel (its Parent), even though it is owned by the form.

PASCAL
procedure TMainForm.AddButtonAtRuntime;
var
  Btn: TButton;
begin
  Btn := TButton.Create(Self);     // Self (the form) owns it: no Free needed
  Btn.Parent := Panel1;             // drawn inside Panel1
  Btn.Caption := 'Dynamic';
  Btn.SetBounds(8, 8, 90, 28);
  Btn.OnClick := GreetButtonClick;  // reuse a handler
end;

Two classic bugs follow from forgetting one of them:

  • No Parent assigned: the button exists but never appears.
  • Create(nil) (no owner) and then forgetting Free: a memory leak.

The runnable program below builds a tree with owners, frees the form, and shows that every owned component is destroyed in turn, with children before their owners.

C Stand-in: an owner frees everything it owns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

You never freed the edit, button or timer yourself; the owner did. That is why designer-placed components need no cleanup code.

Creating and showing forms

The project file (.dpr) creates the forms and starts the message loop:

PASCAL
begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TMainForm, MainForm);
  Application.Run;
end.

For secondary windows, create them on demand and release them when done:

PASCAL
with TSettingsForm.Create(Self) do
  try
    if ShowModal = mrOk then
      ApplySettings;
  finally
    Free;
  end;

(Writing the form in a variable instead of using with reads better.) ShowModal blocks until the dialog closes and returns mrOk, mrCancel, etc.; Show opens a non-modal window and returns at once. Setting ModalResult on a button closes a modal form with that result without any code.

Layout that survives resizing

Do not place everything at fixed pixel positions. Use:

  • Align (alTop, alClient, ...) to dock controls to edges,
  • Anchors to keep distance from edges when the window grows,
  • TPanel, TGridPanel and TFlowPanel for grouping and automatic layout,
  • Constraints for minimum size, and Margins with AlignWithMargins,
  • high-DPI settings in the project options: a form designed at 100% must still look right at 200%.

Habits for clean forms

  • Keep logic out of event handlers: call a method on a separate class or unit, so you can test it without a window.
  • Rename components from Button1 to something meaningful before writing handlers (SaveButton).
  • Free what you create without an owner; trust owners for the rest.
  • Never touch a visual control from a background thread; use TThread.Queue.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main job of a component's Owner?

    Ownership is about lifetime: an owner frees all components it owns.
  2. 2. You create a TButton in code with Create(Self) but it never appears. What is the most likely cause?

    Parent decides where a visible control is drawn. Without it the control exists but is not shown.
  3. 3. What does the .dfm file contain?

    It lists each component and its published property settings, which are streamed in when the form is created.
  4. 4. Why should event handlers stay short?

    Moving logic into ordinary classes makes it testable and keeps forms simple.

Summary

  • Forms and controls are components; TComponent provides names, owners and streaming.
  • Owner decides who frees a component; Parent decides where a control is drawn.
  • The .dfm file stores published properties and event connections, linked with {$R *.dfm}.
  • Create controls in code with Create(Self), then set Parent and properties.
  • Use Align, Anchors and layout panels, and keep logic out of handlers.

Next: Events and the IDE