Delphi L08 Controls at Runtime P2

368 views

Published on

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

No Downloads
Views
Total views
368
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
15
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Delphi L08 Controls at Runtime P2

  1. 1. Intro to Event-driven Programming and Forms with Delphi L08 - Instantiate Controls at Runtime Part 2 and other things you need to know Mohammad Shaker mohammadshakergtr.wordpress.com Intro to Event-driven Programming and Forms with Delphi @ZGTRShaker 2010, 2011, 2012
  2. 2. Instantiate Controls at Runtime
  3. 3. Instantiate Controls at Runtime • Let’s look at the “OnClickButton” procedure TForm2.Button1Click(Sender: TObject); begin end;
  4. 4. Instantiate Controls at Runtime
  5. 5. Instantiate Controls at Runtime • Can we Create Our Own “OnClick” Event? – YES Sure we can – But watch out for defining the exact function header as original one procedure TForm2.ZGTRClick(Sender: TObject); Begin End; Write it yourself
  6. 6. Instantiate Controls at Runtime • Let’s have the following Form
  7. 7. Instantiate Controls at Runtime • And let’s have the following hand-made Event :D procedure TForm2.ZGTRClick(Sender: TObject); var i: integer; Begin ShowMessage('In My Own Event '); End;
  8. 8. Instantiate Controls at Runtime
  9. 9. Instantiate Controls at Runtime procedure TForm2.Button1Click(Sender: TObject); Var myButton:TButton; begin myButton:= TButton.Create(Self); myButton.Parent:= Panel1; myButton.Height:= 35; myButton.Width:= 100; myButton.Caption:= 'MyCreatedButton'; myButton.OnClick:= ZGTRClick; // WOW:D end;
  10. 10. Instantiate Controls at Runtime Before Clicking the Dynamic Button
  11. 11. Instantiate Controls at Runtime • Cool, right? After Clicking the Dynamic Button
  12. 12. Instantiate Controls at Runtime • Another Ex: Let’s Have The following Form
  13. 13. Instantiate Controls at Runtime Var i: integer; // Global Variable procedure TForm2.FormCreate(Sender: TObject); begin i:= 0; // Initialize Variable end; procedure TForm2.ZGTRClick(Sender: TObject); begin ShowMessage('Am in my own created EVENT (procedure) ' + Sender.ClassName); end;
  14. 14. Instantiate Controls at Runtime procedure TForm2.ZGTRClick(Sender: TObject); begin ShowMessage('Am in my own created EVENT (procedure) ' + Sender.ClassName); end;
  15. 15. Instantiate Controls at Runtime procedure TForm2.Button1Click(Sender: TObject); Var myButton:TButton; myLabel:TLabel; myEdit:TEdit; begin myButton:= TButton.Create(Self); myButton.Parent:= Panel1; myButton.Height:= 35; myButton.Width:= 100; myButton.Caption:= 'MyCreatedButton'; myButton.Top:= i*20; myButton.OnClick:= ZGTRClick; i:=i+2;
  16. 16. Instantiate Controls at Runtime myLabel:= TLabel.Create(Self); myLabel.Parent:= Panel1; myLabel.Caption:= 'MyCreatedLabel'; myLabel.Top:= i*20; myLabel.OnClick:= ZGTRClick; i:=i+2; myEdit:= TEdit.Create(Self); myEdit.Parent:= Panel1; myEdit.Text:= 'MyCreatedEdit'; myEdit.Top:= i*20; myEdit.OnClick:= ZGTRClick; end;
  17. 17. Instantiate Controls at Runtime Cool For Now
  18. 18. Instantiate Controls at Runtime Clicking The Button
  19. 19. Instantiate Controls at Runtime Clicking The Label
  20. 20. Instantiate Controls at Runtime Clicking The Edit
  21. 21. Instantiate Controls at Runtime • Another Ex: Let’s have the previous example with the following form and modified “ZGTRClick” is like this.
  22. 22. Instantiate Controls at Runtime procedure TForm2.ZGTRClick(Sender: TObject); var i: integer; Begin i:=i+1; End; procedure TForm2.Button2Click(Sender: TObject); begin ShowMessage('i = ' + IntToStr(i)); end;
  23. 23. Instantiate Controls at Runtime • What will happen now when clicking 4 times on any ones of the 3 controls, and then clicking (Show”i”).
  24. 24. Nice Trick :D • Another Ex: – Let’s see the following Example
  25. 25. Nice Trick :D var i: integer; // Global Variable procedure TForm2.FormCreate(Sender: TObject); begin i:= 0; // Initialize Variable end; procedure TForm2.Button1Click(Sender: TObject); Var myButton:TButton; begin i:= i+1; myButton:= TButton.Create(Self); myButton.Parent:= Panel1; myButton.Top:= i*10; myButton.Left:= 20; myButton.OnClick:= Button1Click; end;
  26. 26. Nice Trick :D Clicking “Create” button two times After That Clicking any ones of created buttons
  27. 27. Array of Objects
  28. 28. Array of Objects • Declaration X: Array of String; M: Array of Array of Integer;
  29. 29. Array of Objects
  30. 30. Array of Objects • Let’s have the following from design
  31. 31. Array of Objects procedure TForm2.Button1Click(Sender: TObject); var ButtonArr: Array of TButton; begin try SetLength(ButtonArr,StrToInt(Edit1.Text)); for I:= 0 to StrToInt(Edit1.Text)-1 do Begin ButtonArr[i]:= TButton.Create(Self); ButtonArr[i].Parent:= Panel1; ButtonArr[i].Top:= i*30; ButtonArr[i].Left:= 10; End; except on e: Exception do ShowMessage('Re-Enter an Integer value.'); end; end;
  32. 32. Array of Objects • Controls created at Runtime!
  33. 33. Exception Handling • Let’s have the following Example
  34. 34. Exception Handling procedure TForm2.Button1Click(Sender: TObject); var Ptr: ^Integer; begin new(Ptr); Edit1.Text:= IntToStr(Ptr^); Ptr^:= 3; Edit1.Text:= IntToStr(Ptr^); end;
  35. 35. Exception Handling procedure TForm2.Button1Click(Sender: TObject); var Ptr: ^Integer; begin Edit1.Text:= IntToStr(Ptr^); Ptr^:= 3; // Here’s The Error Edit1.Text:= IntToStr(Ptr^); end; Runtime Error
  36. 36. Exception Handling procedure TForm2.Button1Click(Sender: TObject); var Ptr: ^Integer; begin Edit1.Text:= IntToStr(Ptr^); end; No Runtime Error
  37. 37. Exception Handling the concept
  38. 38. Exception Handling • The structure of exception handling try // Code which may raise an exception. except on e:Exception do begin // Handle exception “e” end; finally // Code which will be executed whether or not an exception is caught end;
  39. 39. Exception Handling procedure TForm2.Button1Click(Sender: TObject); var Ptr: ^Integer; i: integer; begin try // Note that there’s no need for Begin-End In try Statement Edit1.Text:= IntToStr(Ptr^); Ptr^:= 3; Edit1.Text:= IntToStr(Ptr^); except on e: Exception do ShowMessage('Go away , my program is right , ente el ajdab:D'); end; end; No bugs in my code:@
  40. 40. Exception Handling
  41. 41. Exception Handling • No Msg procedure TForm2.Button1Click(Sender: TObject); var Ptr: ^Integer; i: integer; begin try Edit1.Text:= IntToStr(Ptr^); except on e: Exception do ShowMessage('Go away , my program is right No bugs in my code:@ , ente el ajdab:D'); end; end;
  42. 42. Exception Handling • No Msg procedure TForm2.Button1Click(Sender: TObject); var Ptr: ^Integer; i: integer; begin try i:= Ptr^; except on e: Exception do ShowMessage('Go away , my program is right No bugs in my code:@ , ente el ajdab:D'); end; end;
  43. 43. Exception Handling • Cool! procedure TForm2.Button1Click(Sender: TObject); var i: integer; begin try i:= StrToInt(Edit1.Text); except on e: Exception do ShowMessage(‘ Re-Enter Integer value. ‘); end; end;
  44. 44. Delphi Console Application Can be used for testing, checking algorithms, tracking variables before deploying your project into forms.
  45. 45. Delphi Console Application
  46. 46. Delphi Console Application
  47. 47. Delphi Console Application
  48. 48. Delphi Console Application program Project2; {$APPTYPE CONSOLE} uses SysUtils; begin try { TODO -oUser -cConsole Main: Insert code here } except on E:Exception do Writeln(E.Classname, ': ', E.Message); end; end.
  49. 49. Delphi Console Application program Project2; {$APPTYPE CONSOLE} uses SysUtils; Var x:integer; begin try { TODO -oUser -cConsole Main: Insert code here } except on E:Exception do Writeln(E.Classname, ': ', E.Message); end; Readln(x); Writeln('x = ', x); Readln; end.
  50. 50. Delphi Console Application
  51. 51. And.. end of course
  52. 52. mohammadshakergtr@gmail.com http://mohammadshakergtr.wordpress.com/ tweet @ZGTRShaker
  53. 53. See you!

×