Sabtu, 04 Januari 2014

“Control Properties” dengan Delphi 2010

Control Properties Delphi

Masih berkisar tentang menampilkan masukan dari pengguna tetapi kali ini saya lebih memfokuskan kepada pemakaian tool standar yang terdapat pada Delphi seperti TLabel, TEdit, TButton, TCheckBox, TRadioButton, TComboBox, TScroollBar dan TGroupBox. Aplikasi ini persis sama seperti yang pernah saya terapkan sebelumnya pada Visual Basic 6.0 dan Visual Basic.NET 2010.

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 Control Properties Delphi

Object Name Properties Setting
TForm1 Form1 Border Style
Caption
Position
bsSingle
Control Properties
poScreenCenter
TLabel Label1 Caption Type Your Name:
TEdit edtName Text (Empty)
TLabel lblShow Alignment
Auto Size
Color
Font
taCenter
False
clWhite
Tahoma; 18
TButton btnClickHere Caption Click Here
TGroupBox GroupBox1 Caption Background (RGB)
TScroollBar scrlRed Large Change
Max
30
255
TScroollBar scrlGreen Large Change
Max
30
255
TScroollBar scrlBlue Large Change
Max
30
255
TButton btnExit Caption E&xit
TGroupBox GroupBox2 Caption Font Properties
TLabel Label3 Caption Alignment:
TComboBox cmbAlignment Style csDropDownList
TGroupBox GroupBox3 Caption Color
TRadioButton radRed Caption Red
TRadioButton radGreen Caption Green
TRadioButton radBlue Caption Blue
TRadioButton radYellow Caption Yellow
TGroupBox GroupBox4 Caption Style
TCheckBox chkBold Caption Bold
TCheckBox chkItalic Caption Italic
TCheckBox chkUnderline Caption Underline
TCheckBox chkStrikethrough Caption Strikethrough

5. Berikut event program aplikasi ini:

unit untControlProperties;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    edtName: TEdit;
    lblShow: TLabel;
    btnClickHere: TButton;
    GroupBox1: TGroupBox;
    scrlRed: TScrollBar;
    scrlGreen: TScrollBar;
    scrlBlue: TScrollBar;
    btnExit: TButton;
    GroupBox2: TGroupBox;
    Label3: TLabel;
    cmbAlignment: TComboBox;
    GroupBox3: TGroupBox;
    radRed: TRadioButton;
    radGreen: TRadioButton;
    radBlue: TRadioButton;
    radYellow: TRadioButton;
    GroupBox4: TGroupBox;
    chkBold: TCheckBox;
    chkItalic: TCheckBox;
    chkUnderline: TCheckBox;
    chkStrikethrough: TCheckBox;
    procedure btnClickHereClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnExitClick(Sender: TObject);
    procedure edtNameChange(Sender: TObject);
    procedure edtNameKeyPress(Sender: TObject; var Key: Char);
    procedure DisableFontProperties;
    procedure EnableFontProperties;
    procedure radRedClick(Sender: TObject);
    procedure radGreenClick(Sender: TObject);
    procedure radBlueClick(Sender: TObject);
    procedure radYellowClick(Sender: TObject);
    procedure chkBoldClick(Sender: TObject);
    procedure chkItalicClick(Sender: TObject);
    procedure chkUnderlineClick(Sender: TObject);
    procedure chkStrikethroughClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cmbAlignmentChange(Sender: TObject);
    procedure scrlRedChange(Sender: TObject);
    procedure scrlGreenChange(Sender: TObject);
    procedure scrlBlueChange(Sender: TObject);
    procedure BackgroundRGB;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnClickHereClick(Sender: TObject);
begin
  if btnClickHere.Caption='Click Here' then
  begin
    lblShow.Caption:=edtName.Text;
    edtName.Enabled:=False;
    btnClickHere.Caption:='New';
    btnClickHere.SetFocus;
    EnableFontProperties;
  end
  else if btnClickHere.Caption='New' then
  begin
    edtName.Enabled:=True;
    edtName.Clear;
    lblShow.Caption:='';
    btnClickHere.Caption:='Click Here';
    edtName.SetFocus;
    DisableFontProperties;
  end;
end;

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

procedure TForm1.chkBoldClick(Sender: TObject);
begin
  if chkBold.Checked=True then lblShow.Font.Style:=lblShow.Font.Style+[fsBold]
  else lblShow.Font.Style:=lblShow.Font.Style-[fsBold];
end;

procedure TForm1.chkItalicClick(Sender: TObject);
begin
  if chkItalic.Checked=True then lblShow.Font.Style:=lblShow.Font.Style+[fsItalic]
  else lblShow.Font.Style:=lblShow.Font.Style-[fsItalic]
end;

procedure TForm1.chkStrikethroughClick(Sender: TObject);
begin
  if chkStrikethrough.Checked=True then lblShow.Font.Style:=lblShow.Font.Style+[fsStrikeOut]
  else lblShow.Font.Style:=lblShow.Font.Style-[fsStrikeOut]
end;

procedure TForm1.chkUnderlineClick(Sender: TObject);
begin
  if chkUnderline.Checked=True then lblShow.Font.Style:=lblShow.Font.Style+[fsUnderline]
  else lblShow.Font.Style:=lblShow.Font.Style-[fsUnderline]
end;

procedure TForm1.cmbAlignmentChange(Sender: TObject);
begin
  case cmbAlignment.ItemIndex of
  0: lblShow.Alignment:=taLeftJustify;
  1: lblShow.Alignment:=taCenter;
  2: lblShow.Alignment:=taRightJustify;
  end;
end;

procedure TForm1.edtNameChange(Sender: TObject);
begin
if (edtName.Text<>'') then btnClickHere.Enabled:=True
  else btnClickHere.Enabled:=False;
end;

procedure TForm1.edtNameKeyPress(Sender: TObject; var Key: Char);
begin
  if key=chr(13) then
    btnClickHereClick(btnClickHere);

  if not (key in ['0'..'9']) then
    inherited
  else
    key:=#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.FormCreate(Sender: TObject);
begin
  cmbAlignment.Items.Add('Left Justify');
  cmbAlignment.Items.Add('Center');
  cmbAlignment.Items.Add('Right Justify');
end;

procedure TForm1.radBlueClick(Sender: TObject);
begin
  lblShow.Font.Color:=clBlue;
end;

procedure TForm1.radGreenClick(Sender: TObject);
begin
  lblShow.Font.Color:=clGreen;
end;

procedure TForm1.radRedClick(Sender: TObject);
begin
  lblShow.Font.Color:=clRed;
end;

procedure TForm1.radYellowClick(Sender: TObject);
begin
  lblShow.Font.Color:=clYellow;
end;

procedure TForm1.scrlBlueChange(Sender: TObject);
begin
  BackgroundRGB;
end;

procedure TForm1.scrlGreenChange(Sender: TObject);
begin
  BackgroundRGB;
end;

procedure TForm1.scrlRedChange(Sender: TObject);
begin
  BackgroundRGB;
end;

procedure TForm1.DisableFontProperties;
begin
  cmbAlignment.Enabled:=False;
  radRed.Enabled:=False;
  radGreen.Enabled:=False;
  radBlue.Enabled:=False;
  radYellow.Enabled:=False;
  chkBold.Enabled:=False;
  chkItalic.Enabled:=False;
  chkUnderline.Enabled:=False;
  chkStrikethrough.Enabled:=False;
end;

procedure TForm1.EnableFontProperties;
begin
  cmbAlignment.Enabled:=True;
  radRed.Enabled:=True;
  radGreen.Enabled:=True;
  radBlue.Enabled:=True;
  radYellow.Enabled:=True;
  chkBold.Enabled:=True;
  chkItalic.Enabled:=True;
  chkUnderline.Enabled:=True;
  chkStrikethrough.Enabled:=True;
end;

procedure TForm1.BackgroundRGB;
begin
  Form1.Color:=RGB(scrlRed.Position,scrlGreen.Position,scrlBlue.Position);
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