Delphi uses a What You See Is What You Get (WYSIWYG) approach to UI design. When you run the application, the visual form you designed becomes the main window.
1. The Form Designer and Component Placement
The Form (TForm) is the container for all other visual components.
- Placement: To add a component, select it from the Component Palette and click on the form. The component is automatically named (e.g.,
Button1,Edit1). - Form File (
.dfm): The IDE automatically saves the component layout, size, position, and property values in the binary.dfmfile. You do not edit this file directly. - Unit File (
.pas): The IDE automatically adds a field for the component into the form’s class declaration in the.pasunit.
2. The Object Inspector
The Object Inspector is the primary tool for customizing components at design time. It is split into two tabs:
A. Properties Tab
The Properties tab allows you to set the initial state and appearance of a component.
| Property | Component | Purpose |
Name | TButton, TEdit | The unique identifier used to reference the component in code. (Best practice: use descriptive prefixes like btnSave, edtUserName). |
Caption | TButton, TLabel | The visible text displayed on the component. |
Text | TEdit, TMemo | The current content inside the input field. |
Width, Height | All | The size of the component in pixels. |
Left, Top | All | The component’s position relative to its container (usually the Form). |
Visible | All | Boolean property: determines if the component is displayed to the user. |
B. Events Tab
The Events tab lists all actions the component can respond to. When you double-click an empty event field (e.g., OnClick), the IDE does three things:
- Generates Signature: Creates the method signature (header) in the form’s class declaration (
interface). - Links Event: Assigns the new method to the event in the
.dfmfile. - Opens Code Editor: Jumps to the
implementationsection with the skeleton code block for you to fill in.
3. Event-Driven Programming Model
Delphi applications are event-driven. Execution is driven by external events (user clicks, system notifications, timers) rather than a linear sequence of code.
A. Event Handlers
An Event Handler is a procedure or function that runs automatically when a specific event occurs on a component.
All event handlers follow a standard signature, typically taking a Sender: TObject parameter.
// Procedure generated by the IDE when double-clicking the button's OnClick event
procedure TForm1.btnCalculateClick(Sender: TObject);
begin
// This code executes only when the user clicks btnCalculate
ShowMessage('Calculation started...');
end;
B. The Sender Parameter
The Sender parameter is a reference to the component that triggered the event. This is useful when multiple components share the same event handler.
// Example: Two buttons (btnYes, btnNo) sharing the same handler
procedure TForm1.QuestionButtonClick(Sender: TObject);
begin
// Cast the generic TObject back to a TButton to access its specific properties
if (Sender as TButton).Name = 'btnYes' then
ShowMessage('User said Yes')
else
ShowMessage('User said No');
end;
C. Common Form Events
Forms also have key events that manage the application lifecycle:
| Event | Firing Time | Purpose |
OnCreate | Fired once, when the form is loaded into memory (before it’s visible). | Best place for object initialization and data setup. |
OnShow | Fired right before the form becomes visible. | Used for final UI adjustments. |
OnClose | Fired when the user attempts to close the window. | Used to ask the user to save unsaved work or validate exit conditions. |
OnDestroy | Fired after the form is closed and before its memory is fully freed. | Used to clean up memory or close file handles. |
