delphi l03 forms and input
TRANSCRIPT
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
“Unit”sDividing your programs into district “unit”s is useful for
organizing your project
Peek on our “Delphi code area”
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
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.
Switching between forms
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.
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
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
Switching between formsTest it live
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;
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;
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;
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;
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;
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;
Let’s exercise!
See you!