Tuesday, April 22, 2025

Subtract Two Numbers in Delphi

 A simple program that I wrote using Delphi to subtract two numbers.


Program Listing

unit subtract;


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;

    Label2: TLabel;

    Edit1: TEdit;

    Edit2: TEdit;

    Button1: TButton;

    Label3: TLabel;

    Button2: TButton;

    Button3: TButton;

    Button4: TButton;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    procedure Button3Click(Sender: TObject);

    procedure Button4Click(Sender: TObject);

   private

    { Private declarations }

  public

    { Public declarations }

  end;


var

  Form1: TForm1;


implementation


{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);

var

  Number1, Number2, Result: Double;

begin

        // Convert input strings to numbers

  Number1 := StrToFloat(Edit1.Text);

  Number2 := StrToFloat(Edit2.Text);


  // Perform subtraction

  Result := Number1 - Number2;


  // Display result

  Label3.Caption := 'The difference between ' + FloatToStr(Number1)

  + ' and '+ FloatToStr(Number2) + ' is  '   + FloatToStr(Result) + '.';

end;


procedure TForm1.Button2Click(Sender: TObject);

begin

edit1.Text := '';

edit2.Text := '';

label3.Caption := '';

edit1.SetFocus;

end;


procedure TForm1.Button3Click(Sender: TObject);

begin

if MessageDlg('Are you sure you want to close this program', mtConfirmation,

    [mbYes, mbNo], 0) = mrYes then

  begin

    Close;  // This will unload/close the form

  end

  else

   begin

     edit1.Text := '';

     edit2.Text := '';

     label3.Caption := '';

     edit1.SetFocus;

   end;

end;



procedure TForm1.Button4Click(Sender: TObject);

begin


  // Show message box with custom icon

  Application.MessageBox(

    'Created By Dr. Jake Rodriguez Pomperada,PHD-TM',

    'About this program',

    MB_OK + MB_ICONINFORMATION

  );

end;


end.


No comments:

Post a Comment