A simple desktop application using Delphi. This example will be a basic calculator application

with a user interface for addition, subtraction, multiplication, and division.

Here's how you can create a simple calculator app in Delphi:neema blog 5

  1. Open Delphi IDE and create a new VCL Forms Application project.

  2. Design the user interface:

    • Place a TEdit component for input.
    • Place four TButton components for addition, subtraction, multiplication, and division operations.
    • Place a TLabel component to display the result.
  3. Add event handlers for the button clicks to perform arithmetic operations based on user input.

Here's the Delphi code for the main form unit (MainForm.pas):

delphi
unit MainForm;

interface

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

type
  TMainCalculatorForm = class(TForm)
    edtInput1: TEdit;
    edtInput2: TEdit;
    btnAdd: TButton;
    btnSubtract: TButton;
    btnMultiply: TButton;
    btnDivide: TButton;
    lblResult: TLabel;
    procedure btnAddClick(Sender: TObject);
    procedure btnSubtractClick(Sender: TObject);
    procedure btnMultiplyClick(Sender: TObject);
    procedure btnDivideClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainCalculatorForm: TMainCalculatorForm;

implementation

{$R *.dfm}

procedure TMainCalculatorForm.btnAddClick(Sender: TObject);
var
  Input1, Input2, Result: Double;
begin
  Input1 := StrToFloat(edtInput1.Text);
  Input2 := StrToFloat(edtInput2.Text);
  Result := Input1 + Input2;
  lblResult.Caption := Format('Result: %.2f', [Result]);
end;

procedure TMainCalculatorForm.btnSubtractClick(Sender: TObject);
var
  Input1, Input2, Result: Double;
begin
  Input1 := StrToFloat(edtInput1.Text);
  Input2 := StrToFloat(edtInput2.Text);
  Result := Input1 - Input2;
  lblResult.Caption := Format('Result: %.2f', [Result]);
end;

procedure TMainCalculatorForm.btnMultiplyClick(Sender: TObject);
var
  Input1, Input2, Result: Double;
begin
  Input1 := StrToFloat(edtInput1.Text);
  Input2 := StrToFloat(edtInput2.Text);
  Result := Input1 * Input2;
  lblResult.Caption := Format('Result: %.2f', [Result]);
end;

procedure TMainCalculatorForm.btnDivideClick(Sender: TObject);
var
  Input1, Input2, Result: Double;
begin
  Input1 := StrToFloat(edtInput1.Text);
  Input2 := StrToFloat(edtInput2.Text);
  if Input2 <> 0 then
    Result := Input1 / Input2
  else
    Result := 0;
  lblResult.Caption := Format('Result: %.2f', [Result]);
end;

end.


In this example, we have four buttons (btnAdd, btnSubtract, btnMultiply, btnDivide), two edit boxes (edtInput1, edtInput2), and a label (lblResult). Each button has an event handler that performs the corresponding arithmetic operation based on the values entered in the edit boxes and displays the result in the label.

This is a simple example to demonstrate the basics of creating a Delphi application. You can further enhance this calculator application by adding error handling, validation, and additional features as needed.

If you like the article and in need fo such a service, please dont hesitate to      contact us