Delphi L03 Forms and Input

534 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
534
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
22
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Delphi L03 Forms and Input

  1. 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
  2. 2. “Unit”s Dividing your programs into district “unit”s is useful for organizing your project
  3. 3. Peek on our “Delphi code area”
  4. 4. 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
  5. 5. 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.
  6. 6. Switching between forms
  7. 7. 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.
  8. 8. 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
  9. 9. 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
  10. 10. Switching between forms Test it live
  11. 11. 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;
  12. 12. 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;
  13. 13. 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;
  14. 14. 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;
  15. 15. 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;
  16. 16. 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;
  17. 17. Let’s exercise!
  18. 18. See you!

×