Event-driven programming is a paradigm in which the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or messages from other programs. In Delphi, event-driven programming is a core concept, especially when developing graphical user interfaces (GUIs) with the Visual Component Library (VCL) or FireMonkey (FMX).

Key Concepts

  1. Events: Actions or occurrences that happen during the execution of a program. Examples include clicking a button, moving the mouse, or pressing a key.
  2. Event Handlers: Procedures or functions that are executed in response to an event. They contain the code that defines what should happen when an event occurs.
  3. Event Loop: A loop that waits for events and dispatches them to the appropriate event handlers.

Understanding Events and Event Handlers

In Delphi, events are properties of components that can be assigned to event handlers. Event handlers are methods that respond to specific events.

Example: Button Click Event

Let's create a simple Delphi application with a button that displays a message when clicked.

  1. Create a New VCL Application:

    • Open Delphi.
    • Select File > New > VCL Forms Application - Delphi.
  2. Add a Button to the Form:

    • From the Tool Palette, drag a TButton component onto the form.
  3. Assign an Event Handler to the Button:

    • Double-click the button to create an OnClick event handler. Delphi will automatically generate a method in the form's unit file.

Code 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;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

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

end.

Explanation

  • TForm1: The main form class.
  • Button1Click: The event handler for the button's OnClick event. This method is executed when the button is clicked.
  • ShowMessage: A standard Delphi procedure that displays a message box with the specified text.

Practical Exercise

Exercise 1: Create a Simple Calculator

Create a simple calculator application that performs basic arithmetic operations (addition, subtraction, multiplication, division) using buttons and event handlers.

Steps

  1. Create a New VCL Application.

  2. Design the Form:

    • Add four TButton components for the operations (+, -, *, /).
    • Add two TEdit components for input numbers.
    • Add a TLabel component to display the result.
  3. Assign Event Handlers:

    • Double-click each button to create event handlers for their OnClick events.

Code 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)
    Edit1: TEdit;
    Edit2: TEdit;
    ButtonAdd: TButton;
    ButtonSubtract: TButton;
    ButtonMultiply: TButton;
    ButtonDivide: TButton;
    LabelResult: TLabel;
    procedure ButtonAddClick(Sender: TObject);
    procedure ButtonSubtractClick(Sender: TObject);
    procedure ButtonMultiplyClick(Sender: TObject);
    procedure ButtonDivideClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ButtonAddClick(Sender: TObject);
var
  Num1, Num2, Result: Double;
begin
  Num1 := StrToFloat(Edit1.Text);
  Num2 := StrToFloat(Edit2.Text);
  Result := Num1 + Num2;
  LabelResult.Caption := 'Result: ' + FloatToStr(Result);
end;

procedure TForm1.ButtonSubtractClick(Sender: TObject);
var
  Num1, Num2, Result: Double;
begin
  Num1 := StrToFloat(Edit1.Text);
  Num2 := StrToFloat(Edit2.Text);
  Result := Num1 - Num2;
  LabelResult.Caption := 'Result: ' + FloatToStr(Result);
end;

procedure TForm1.ButtonMultiplyClick(Sender: TObject);
var
  Num1, Num2, Result: Double;
begin
  Num1 := StrToFloat(Edit1.Text);
  Num2 := StrToFloat(Edit2.Text);
  Result := Num1 * Num2;
  LabelResult.Caption := 'Result: ' + FloatToStr(Result);
end;

procedure TForm1.ButtonDivideClick(Sender: TObject);
var
  Num1, Num2, Result: Double;
begin
  Num1 := StrToFloat(Edit1.Text);
  Num2 := StrToFloat(Edit2.Text);
  if Num2 <> 0 then
  begin
    Result := Num1 / Num2;
    LabelResult.Caption := 'Result: ' + FloatToStr(Result);
  end
  else
    ShowMessage('Cannot divide by zero');
end;

end.

Explanation

  • Edit1 and Edit2: Input fields for the numbers.
  • ButtonAddClick, ButtonSubtractClick, ButtonMultiplyClick, ButtonDivideClick: Event handlers for the respective buttons.
  • LabelResult: Displays the result of the operation.

Common Mistakes and Tips

  • Invalid Input: Ensure that the input fields contain valid numbers. Use TryStrToFloat to handle invalid input gracefully.
  • Division by Zero: Always check for division by zero to avoid runtime errors.

Summary

In this section, you learned about event-driven programming in Delphi, focusing on events, event handlers, and the event loop. You created a simple application to demonstrate how to handle button click events and built a basic calculator to practice these concepts. Event-driven programming is essential for creating interactive applications, and mastering it will significantly enhance your Delphi programming skills.

Next, we will explore more advanced topics in GUI development with VCL and FMX.

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