Minggu, 05 Januari 2014

“Simple Calculator” dengan Delphi 2010

Simple Calculator Delphi

Setelah sebelumnya saya adaptasikan dengan Visual Baic 6.0 dan Visual Basic.NET 2010 sekarang saya terapkan dengan Delphi 2010. Baik logika program dan tampilan form tidak ada yang dirubah. Jadi bila teman-teman dapat membedakan secara langsung dimana letak perbedaan diantara ketiga bahasa pemrograman. Mana yang menurut teman-teman lebih mudah di pergunakan? silahkan teman-teman tentukan sendiri.


1. Jalankan terlebih dahulu aplikasi Delphi 2010

2. Pada jendela “Welcome Page” pilih  “New Project…

New Project Delphi

3. Pada jendela “New Items” pilih “Delphi projects” lalu pilih “VCL Forms Application” kemudian pilih “OK

New Items Delphi

4. Berikut seting kontrol properti yang terdapat pada aplikasi ini:

LayOut Simple Calculator Delphi

Object Name Properties Setting
TForm1 Form1 Border Style
Caption
Position
bsSingle
Simple Calculator
poScreenCenter
TLabel Label1 Caption First Number:
TEdit edtFirst Alignment
Text
taRightJustify
(Empty)
TLabel Label2 Caption Second Number:
TEdit edtSecond Alignment
Text
taRightJustify
(Empty)
TButton btnAdd Caption Add
TButton btnSubtract Caption Subtract
TButton btnMultiple Caption Multiple
TButton btnDivide Caption Divide
TLabel Label3 Caption Result:
TEdit edtResult Alignment
Text
taRightJustify
(Empty)
TButton btnClear Caption Clear
TButton btnExit Caption E&xit

5. Berikut event program aplikasi ini:

unit untSimpleCalculator;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    edtFirst: TEdit;
    Label2: TLabel;
    edtSecond: TEdit;
    btnAdd: TButton;
    btnSubtract: TButton;
    btnMultiple: TButton;
    btnDivide: TButton;
    Label3: TLabel;
    edtResult: TEdit;
    btnClear: TButton;
    btnExit: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnExitClick(Sender: TObject);
    procedure edtFirstKeyPress(Sender: TObject; var Key: Char);
    procedure edtSecondKeyPress(Sender: TObject; var Key: Char);
    procedure edtSecondChange(Sender: TObject);
    procedure DisableButton;
    procedure EnableButton;
    procedure btnClearClick(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
    procedure btnSubtractClick(Sender: TObject);
    procedure btnMultipleClick(Sender: TObject);
    procedure btnDivideClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnAddClick(Sender: TObject);
begin
  edtResult.Text:=FloatToStr(StrToFloat(edtFirst.Text) + StrToFloat(edtSecond.Text));
end;

procedure TForm1.btnClearClick(Sender: TObject);
begin
  edtFirst.Clear;
  edtSecond.Clear;
  edtResult.Clear;
  DisableButton;
  edtFirst.SetFocus;
end;

procedure TForm1.btnDivideClick(Sender: TObject);
begin
  edtResult.Text:=FloatToStr(StrToFloat(edtFirst.Text) / StrToFloat(edtSecond.Text));
end;

procedure TForm1.btnExitClick(Sender: TObject);
begin
  Close;
end;

procedure TForm1.btnMultipleClick(Sender: TObject);
begin
  edtResult.Text:=FloatToStr(StrToFloat(edtFirst.Text) * StrToFloat(edtSecond.Text));
end;

procedure TForm1.btnSubtractClick(Sender: TObject);
begin
  edtResult.Text:=FloatToStr(StrToFloat(edtFirst.Text) - StrToFloat(edtSecond.Text));
end;

procedure TForm1.edtFirstKeyPress(Sender: TObject; var Key: Char);
begin
  if (key in ['0'..'9',#8,#13,'-','.']) then inherited
  else key:=#0;

  if key=chr(13) then Perform(WM_NEXTDLGCTL, 0, 0);
end;

procedure TForm1.edtSecondChange(Sender: TObject);
begin
  if (edtSecond.Text<>'') then EnableButton
  else DisableButton;
end;

procedure TForm1.edtSecondKeyPress(Sender: TObject; var Key: Char);
begin
  if (key in ['0'..'9',#8,#13,'-','.']) then inherited
  else key:=#0;

  if key=chr(13) then Perform(WM_NEXTDLGCTL, 0, 0);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  case MessageDlg('Exit program',mtConfirmation,[mbYes, mbNo],0) of
    mrOk: Close;
    mrNo: Action:=caNone;
  end;
end;

procedure TForm1.DisableButton;
begin
  btnAdd.Enabled:=False;
  btnSubtract.Enabled:=False;
  btnMultiple.Enabled:=False;
  btnDivide.Enabled:=False;
  edtResult.Enabled:=False;
  btnClear.Enabled:=False;
end;

procedure TForm1.EnableButton;
begin
  btnAdd.Enabled:=True;
  btnSubtract.Enabled:=True;
  btnMultiple.Enabled:=True;
  btnDivide.Enabled:=True;
  edtResult.Enabled:=True;
  btnClear.Enabled:=True;
end;

end.

6. Pilih tombol “Run” atau teman-teman bisa langsung menekan tombol “F9” pada keyboard

Run Delphi

Label:

0 Komentar:

Posting Komentar

Berlangganan Posting Komentar [Atom]

<< Beranda