2024: Domino Containers - The Next Step. News from the Domino Container commu...
Delphi L03 Forms and Input
1. Intro to Event-driven Programming
and Forms with Delphi
L03 – Forms and Input
Mohammad Shaker
mohammadshakergtr.wordpress.com
Intro to Event-driven Programming and Forms with Delphi
@ZGTRShaker
2010, 2011, 2012
5. Peek on our “Delphi code area”
unit Unit1;
interface
Name of the “unit” we are working in
Libraries we can take functions &
procedures from it
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.
Types & classes
Public Variables
Here begins the “unit”
implementation
6. unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
end;
end.
8. 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.
9. 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
10. Switching between forms
“Events & Properties”
• Some Events:
–
–
–
–
–
Form1.hide;
Form1.show;
Form1.OnClose;
Form1.OnCreate;
Form1.Refresh;
// imp.
// imp.
// imp.
• Some Properties:
– Design time:
• Visible, Enabled, etc.
// Design time
– Runtime:
• Form1.Visibe:= True/False;
• Form1.Enabled:= True/False;
// Runtime
// Runtime
12. KeyBoard Response “Event”s
• Event (Code Sample):
procedure TForm1.FormKeyDown(Sender: TObject; var Key:
Word;Shift: TShiftState);
Begin
if (key = VK_Right) then
// code
else
if (key = VK_Left) then
//code
end;
13. 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;
14. 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;
15. 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;
16. 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;
17. 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;