delphi l08 controls at runtime p2
TRANSCRIPT
![Page 1: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/1.jpg)
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
L08 - Instantiate Controls at RuntimePart 2 and other things you need to
know
![Page 2: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/2.jpg)
![Page 3: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/3.jpg)
Instantiate Controls at Runtime
![Page 4: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/4.jpg)
Instantiate Controls at Runtime
• Let’s look at the “OnClickButton”
procedure TForm2.Button1Click(Sender: TObject);
begin
end;
![Page 5: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/5.jpg)
Instantiate Controls at Runtime
![Page 6: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/6.jpg)
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
![Page 7: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/7.jpg)
Instantiate Controls at Runtime
• Let’s have the following Form
![Page 8: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/8.jpg)
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;
![Page 9: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/9.jpg)
Instantiate Controls at Runtime
![Page 10: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/10.jpg)
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;
![Page 11: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/11.jpg)
Instantiate Controls at Runtime
Before Clicking the Dynamic Button
![Page 12: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/12.jpg)
Instantiate Controls at Runtime
• Cool, right?
After Clicking the Dynamic Button
![Page 13: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/13.jpg)
Instantiate Controls at Runtime
• Another Ex: Let’s Have The following Form
![Page 14: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/14.jpg)
Instantiate Controls at Runtime
Var
i: integer; // Global Variable
procedure TForm2.ZGTRClick(Sender: TObject);
begin
ShowMessage('Am in my own created EVENT (procedure) '
+ Sender.ClassName);
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
i:= 0; // Initialize Variable
end;
![Page 15: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/15.jpg)
Instantiate Controls at Runtime
procedure TForm2.ZGTRClick(Sender: TObject);
begin
ShowMessage('Am in my own created EVENT (procedure) '
+ Sender.ClassName);
end;
![Page 16: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/16.jpg)
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;
![Page 17: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/17.jpg)
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;
![Page 18: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/18.jpg)
Instantiate Controls at Runtime
Cool For Now
![Page 19: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/19.jpg)
Instantiate Controls at Runtime
Clicking The Button
![Page 20: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/20.jpg)
Instantiate Controls at Runtime
Clicking The Label
![Page 21: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/21.jpg)
Instantiate Controls at Runtime
Clicking The Edit
![Page 22: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/22.jpg)
Instantiate Controls at Runtime
• Another Ex: Let’s have the previous example with the following form and modified “ZGTRClick” is like this.
![Page 23: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/23.jpg)
Instantiate Controls at Runtime
procedure TForm2.Button2Click(Sender: TObject);
begin
ShowMessage('i = ' + IntToStr(i));
end;
procedure TForm2.ZGTRClick(Sender: TObject);
var i: integer;
Begin
i:=i+1;
End;
![Page 24: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/24.jpg)
Instantiate Controls at Runtime
• What will happen now when clicking 4 times on any ones of the 3 controls, and then clicking (Show”i”).
![Page 25: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/25.jpg)
Nice Trick :D
• Another Ex:– Let’s see the following Example
![Page 26: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/26.jpg)
Nice Trick :D
var i: integer; // Global Variable
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;
procedure TForm2.FormCreate(Sender: TObject);
begin
i:= 0; // Initialize Variable
end;
![Page 27: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/27.jpg)
Nice Trick :D
Clicking “Create” button two times
After ThatClicking any ones
of created buttons
![Page 28: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/28.jpg)
Array of Objects
![Page 29: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/29.jpg)
Array of Objects
• Declaration
X: Array of String;
M: Array of Array of Integer;
![Page 30: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/30.jpg)
Array of Objects
![Page 31: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/31.jpg)
Array of Objects
• Let’s have the following from design
![Page 32: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/32.jpg)
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;
![Page 33: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/33.jpg)
Array of Objects
• Controls created at Runtime!
![Page 34: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/34.jpg)
![Page 35: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/35.jpg)
Exception Handling
• Let’s have the following Example
![Page 36: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/36.jpg)
Exception Handling
procedure TForm2.Button1Click(Sender: TObject);
var Ptr: ^Integer;
begin
new(Ptr);
Edit1.Text:= IntToStr(Ptr^);
Ptr^:= 3;
Edit1.Text:= IntToStr(Ptr^);
end;
![Page 37: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/37.jpg)
Exception Handling
procedure TForm2.Button1Click(Sender: TObject);
var Ptr: ^Integer;
begin
Edit1.Text:= IntToStr(Ptr^);
Ptr^:= 3;
Edit1.Text:= IntToStr(Ptr^);
end;
Runtime Error
// Here’s The Error
![Page 38: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/38.jpg)
Exception Handling
procedure TForm2.Button1Click(Sender: TObject);
var Ptr: ^Integer;
begin
Edit1.Text:= IntToStr(Ptr^);
end;
No Runtime Error
![Page 39: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/39.jpg)
Exception Handlingthe concept
![Page 40: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/40.jpg)
Exception Handling
• The structure of exception handlingtry
// 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;
![Page 41: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/41.jpg)
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 No bugs in my code:@
, ente el ajdab:D');
end;
end;
![Page 42: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/42.jpg)
Exception Handling
![Page 43: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/43.jpg)
Exception Handling
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;
• No Msg
![Page 44: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/44.jpg)
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;
![Page 45: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/45.jpg)
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;
![Page 46: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/46.jpg)
Delphi Console ApplicationCan be used for testing, checking algorithms, tracking
variables before deploying your project into forms.
![Page 47: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/47.jpg)
Delphi Console Application
![Page 48: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/48.jpg)
Delphi Console Application
![Page 49: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/49.jpg)
Delphi Console Application
![Page 50: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/50.jpg)
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.
![Page 51: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/51.jpg)
Delphi Console Applicationprogram 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.
![Page 52: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/52.jpg)
Delphi Console Application
![Page 53: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/53.jpg)
![Page 54: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/54.jpg)
And.. end of course
![Page 56: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/56.jpg)
![Page 57: Delphi L08 Controls at Runtime P2](https://reader034.vdocuments.net/reader034/viewer/2022052311/557a92f5d8b42ac8638b5414/html5/thumbnails/57.jpg)
See you!