Your SlideShare is downloading. ×
0
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Delphi L08 Controls at Runtime P2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Delphi L08 Controls at Runtime P2

118

Published on

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

No Downloads
Views
Total Views
118
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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. Instantiate Controls at Runtime
  • 3. Instantiate Controls at Runtime • Let’s look at the “OnClickButton” procedure TForm2.Button1Click(Sender: TObject); begin end;
  • 4. Instantiate Controls at Runtime
  • 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. Instantiate Controls at Runtime • Let’s have the following Form
  • 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. Instantiate Controls at Runtime
  • 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. Instantiate Controls at Runtime Before Clicking the Dynamic Button
  • 11. Instantiate Controls at Runtime • Cool, right? After Clicking the Dynamic Button
  • 12. Instantiate Controls at Runtime • Another Ex: Let’s Have The following Form
  • 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. Instantiate Controls at Runtime procedure TForm2.ZGTRClick(Sender: TObject); begin ShowMessage('Am in my own created EVENT (procedure) ' + Sender.ClassName); end;
  • 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. 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. Instantiate Controls at Runtime Cool For Now
  • 18. Instantiate Controls at Runtime Clicking The Button
  • 19. Instantiate Controls at Runtime Clicking The Label
  • 20. Instantiate Controls at Runtime Clicking The Edit
  • 21. Instantiate Controls at Runtime • Another Ex: Let’s have the previous example with the following form and modified “ZGTRClick” is like this.
  • 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. 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. Nice Trick :D • Another Ex: – Let’s see the following Example
  • 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. Nice Trick :D Clicking “Create” button two times After That Clicking any ones of created buttons
  • 27. Array of Objects
  • 28. Array of Objects • Declaration X: Array of String; M: Array of Array of Integer;
  • 29. Array of Objects
  • 30. Array of Objects • Let’s have the following from design
  • 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. Array of Objects • Controls created at Runtime!
  • 33. Exception Handling • Let’s have the following Example
  • 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. 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. Exception Handling procedure TForm2.Button1Click(Sender: TObject); var Ptr: ^Integer; begin Edit1.Text:= IntToStr(Ptr^); end; No Runtime Error
  • 37. Exception Handling the concept
  • 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. 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. Exception Handling
  • 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. 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. 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. Delphi Console Application Can be used for testing, checking algorithms, tracking variables before deploying your project into forms.
  • 45. Delphi Console Application
  • 46. Delphi Console Application
  • 47. Delphi Console Application
  • 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. 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. Delphi Console Application
  • 51. And.. end of course
  • 52. mohammadshakergtr@gmail.com http://mohammadshakergtr.wordpress.com/ tweet @ZGTRShaker
  • 53. See you!

×