In this section, we will delve into the process of creating forms and controls in Delphi using the Visual Component Library (VCL). This is a fundamental skill for building graphical user interfaces (GUIs) in Delphi applications.

Objectives

By the end of this section, you will be able to:

  1. Create and customize forms.
  2. Add and configure various controls on forms.
  3. Understand the properties, methods, and events associated with forms and controls.

  1. Creating a New Form

Step-by-Step Guide

  1. Open Delphi IDE: Start the Delphi Integrated Development Environment (IDE).

  2. Create a New Project:

    • Go to File > New > VCL Forms Application - Delphi.
    • This will create a new project with a default form named Form1.
  3. Customize the Form:

    • Select the form in the designer.
    • Use the Object Inspector to modify properties such as Name, Caption, Width, and Height.

Example

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 this example, TForm1 is the class representing the form. The {$R *.dfm} directive links the form's design file.

  1. Adding Controls to the Form

Common Controls

  • Button: Used to trigger actions.
  • Label: Displays text.
  • Edit: Allows user input.
  • ComboBox: Provides a dropdown list.
  • ListBox: Displays a list of items.

Adding Controls

  1. Select a Control: From the Tool Palette, select the control you want to add (e.g., TButton).
  2. Place the Control: Click on the form where you want to place the control.
  3. Configure the Control: Use the Object Inspector to set properties like Name, Caption, Text, etc.

Example

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := 'Hello, ' + Edit1.Text;
end;

end.

In this example:

  • A TButton, TLabel, and TEdit control are added to the form.
  • The Button1Click procedure is an event handler that updates the label's caption with the text entered in the edit control when the button is clicked.

  1. Properties, Methods, and Events

Properties

Properties define the characteristics of controls. For example:

  • Caption: The text displayed on a button or label.
  • Text: The text contained in an edit control.
  • Enabled: Determines if the control is interactive.

Methods

Methods are functions or procedures that perform actions. For example:

  • Show: Displays the form.
  • Hide: Hides the form.

Events

Events are actions triggered by user interactions or other occurrences. For example:

  • OnClick: Triggered when a button is clicked.
  • OnChange: Triggered when the text in an edit control changes.

Example

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage('Button clicked!');
end;

In this example, the Button1Click event handler displays a message box when the button is clicked.

Practical Exercise

Task

Create a form with the following controls:

  • A TLabel with the caption "Enter your name:".
  • A TEdit for user input.
  • A TButton with the caption "Greet".
  • When the button is clicked, display a message box greeting the user by name.

Solution

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage('Hello, ' + Edit1.Text + '!');
end;

end.

Explanation

  • The Label1 prompts the user to enter their name.
  • The Edit1 control allows the user to input their name.
  • The Button1Click event handler displays a greeting message using the text from Edit1.

Conclusion

In this section, you learned how to create and customize forms and controls in Delphi. You also explored the properties, methods, and events associated with these components. This foundational knowledge is crucial for building interactive and user-friendly applications in Delphi. In the next section, we will delve into event-driven programming, which will further enhance your ability to create responsive applications.

Delphi/Object Pascal Programming Course

Module 1: Introduction to Delphi/Object Pascal

Module 2: Control Structures and Procedures

Module 3: Working with Data

Module 4: Object-Oriented Programming

Module 5: Advanced Delphi Features

Module 6: GUI Development with VCL and FMX

Module 7: Web and Mobile Development

Module 8: Best Practices and Design Patterns

Module 9: Final Project

© Copyright 2024. All rights reserved