delphi l03 forms and input

19
Mohammad Shaker mohammadshakergtr.wordpress.com Intro to Event-driven Programming and Forms with Delphi @ZGTRShaker 2010, 2011, 2012 Intro to Event-driven Programming and Forms with Delphi L03 – Forms and Input

Upload: mohammad-shaker

Post on 14-May-2015

204 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Delphi L03 Forms and Input

Mohammad Shakermohammadshakergtr.wordpress.com

Intro to Event-driven Programming and Forms with Delphi@ZGTRShaker

2010, 2011, 2012

Intro to Event-driven Programming and Forms with Delphi

L03 – Forms and Input

Page 2: Delphi L03 Forms and Input
Page 3: Delphi L03 Forms and Input

“Unit”sDividing your programs into district “unit”s is useful for

organizing your project

Page 4: Delphi L03 Forms and Input

Peek on our “Delphi code area”

Page 5: Delphi L03 Forms and Input

Peek on our “Delphi code area”unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs;

type

TForm1 = class(TForm)

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

end.

Name of the “unit” we are working in

Here begins the “unit”

implementation

Public Variables

Types & classes

Libraries we can take functions &

procedures from it

Page 6: Delphi L03 Forms and Input

unit Unit1;

interface

usesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs;

typeTForm1 = class(TForm)Button1: TButton;procedure Button1Click(Sender: TObject);private{ Private declarations }public{ Public declarations }end;

varForm1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);begin

end;

end.

Page 7: Delphi L03 Forms and Input

Switching between forms

Page 8: Delphi L03 Forms and Input

Switching between forms

• Create a new “form” in our “current” project:– File (Menu) > new > form

• Select the main application form:– Project (Menu) > Options > Forms > Main Form– select the “main” application form.

Page 9: Delphi L03 Forms and Input

Switching between forms

• Linking forms: – Every form has its own “unit”:

• Form1 > unit1.pas• Form2 > unit2.pas

– If we want:• form1 to use form2, we add to unit1 (NOT THE CONTRARY):

– uses unit2

• form2 to use form1, we add to unit2 (NOT THE CONTRARY):– uses unit1

– Watch out • for “Circular declaration” in interfaces

– i.e: uses unit1 & uses unit2 at the same time in unit

Page 10: Delphi L03 Forms and Input

Switching between forms“Events & Properties”• Some Events:

– Form1.hide;– Form1.show;– Form1.OnClose; // imp.– Form1.OnCreate; // imp.– Form1.Refresh; // imp.

• Some Properties:– Design time:

• Visible, Enabled, etc. // Design time – Runtime:

• Form1.Visibe:= True/False; // Runtime• Form1.Enabled:= True/False; // Runtime

Page 11: Delphi L03 Forms and Input

Switching between formsTest it live

Page 12: Delphi L03 Forms and Input

KeyBoard Response “Event”s

• Event (Code Sample):

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);Begin

if (key = VK_Right) then// code

elseif (key = VK_Left) then

//code

end;

Page 13: Delphi L03 Forms and Input

KeyBoard Response “Event”s

• Event (1st Code Example): (KeyDown: Down)

procedure TForm2.FormKeyDown(Sender: TObject; var Key: Word;

Shift: TShiftState);

begin

Label1.Caption:='0';

if (key = VK_RIGHT) then

label1.Caption:='1'

else

if (key=VK_LEFT) then

Label1.Caption:='2'

else

if (key=VK_DOWN) then

Label1.Caption:='3';

end;

Page 14: Delphi L03 Forms and Input

KeyBoard Response “Event”s

• Event (2nd Code Example): (KeyDown: UP)

procedure TForm2.FormKeyDown(Sender: TObject; var Key: Word;

Shift: TShiftState);

begin

Label1.Caption:='0';

if (key = VK_RIGHT) then

label1.Caption:='1'

else

if (key=VK_LEFT) then

Label1.Caption:='2'

else

if (key=VK_DOWN) then

Label1.Caption:='3';

end;

Page 15: Delphi L03 Forms and Input

KeyBoard Response “Event”s

• Event (3rd Code Example): (KeyDown: Left)

procedure TForm2.FormKeyDown(Sender: TObject; var Key: Word;

Shift: TShiftState);

var bool1:boolean;

begin

bool1:=false;

Label1.Caption:='0';

if ((key = VK_RIGHT) and (bool1=not(false))) then

label1.Caption:='1'

else

if (bool1=false) then

Label1.Caption:='2'

else

Label1.Caption:='3';

end;

Page 16: Delphi L03 Forms and Input

KeyBoard Response “Event”s

• Event (4th Code Example): (KeyDown: Down)

procedure TForm2.FormKeyDown(Sender: TObject; var Key: Word;

Shift: TShiftState);

var bool1:boolean;

begin

bool1:=true;

Label1.Caption:='0';

if ((key = VK_RIGHT) or (bool1=not(false))) then

label1.Caption:='1'

else

if (key=VK_LEFT) then

Label1.Caption:='2'

else

if (key=VK_DOWN) then

Label1.Caption:='3';

end;

Page 17: Delphi L03 Forms and Input

KeyBoard Response “Event”s

• Event (5th Code Example): (KeyDown: Down)

procedure TForm2.FormKeyDown(Sender: TObject; var Key: Word;

Shift: TShiftState);

var bool1:boolean;

begin

bool1:=true;

Label1.Caption:='0';

if ((key = VK_RIGHT) and (bool1=not(false))) then

label1.Caption:='1'

else

if (key=VK_LEFT) then

Label1.Caption:='2'

else

if (key=VK_DOWN) then

Label1.Caption:='3';

end;

Page 18: Delphi L03 Forms and Input

Let’s exercise!

Page 19: Delphi L03 Forms and Input

See you!