Monday, November 4, 2024

Age Checker in Delphi

 



Program Listing

unit demo;


interface


uses

  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,

  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ToolWin, Vcl.ComCtrls;


type

  TForm1 = class(TForm)

    StatusBar1: TStatusBar;

    ToolBar1: TToolBar;

    Edit1: TEdit;

    Label1: TLabel;

    Label2: TLabel;

    Button1: TButton;

    Button2: TButton;

    Button3: TButton;

    procedure Button1Click(Sender: TObject);

    procedure FormCreate(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    procedure Button3Click(Sender: TObject);

  private

    { Private declarations }

     function AskToQuit: Boolean;

  public

    { Public declarations }

  end;


var

  Form1: TForm1;


implementation


{$R *.dfm}


// Programmer : Dr. Jake R. Pomperada

// Date : November 4, 2024


procedure TForm1.Button1Click(Sender: TObject);

var

  age: Integer;

begin

  try

    age := StrToInt(edit1.Text);


    if age >= 18 then

      label2.Caption := 'You are an adult. At age of ' +IntToStr(age) + ' years old.'

    else

      label2.Caption := 'You are a minor.  At age of ' +IntToStr(age) + ' years old.';

  except

    on EConvertError do

      ShowMessage('Please enter a valid age (a number).');

  end;

end;



procedure TForm1.Button2Click(Sender: TObject);

begin

label2.Caption := '';

edit1.text := '';

edit1.SetFocus;

end;


procedure TForm1.Button3Click(Sender: TObject);

begin

if AskToQuit then

    Close // Close the form if user confirms

    else

      edit1.SetFocus;

end;


function TForm1.AskToQuit: Boolean;

begin

  Result := MessageDlg('Are you sure you want to quit?', mtConfirmation, [mbYes, mbNo], 0) = mrYes;

end;


procedure TForm1.FormCreate(Sender: TObject);

begin

 // Optionally adjust the form position here if needed

  // For example, move it slightly to the left and up

  Left := (Screen.Width - Width) div 2;   // Center horizontally

  Top := (Screen.Height - Height) div 2;  // Center vertically

end;


end.


No comments:

Post a Comment