Mohammad Shaker mohammadshakergtr.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 Runtime Part 2 and other things you need to know
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
Instantiate Controls at Runtime
Instantiate Controls at Runtime
• Let’s look at the “OnClickButton”
procedure TForm2.Button1Click(Sender: TObject);
begin
end;
Instantiate Controls at Runtime
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
Instantiate Controls at Runtime
• Let’s have the following Form
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;
Instantiate Controls at Runtime
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;
Instantiate Controls at Runtime
Before Clicking the Dynamic Button
Instantiate Controls at Runtime
• Cool, right?
After Clicking the Dynamic Button
Instantiate Controls at Runtime
• Another Ex: Let’s Have The following Form
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;
Instantiate Controls at Runtime
procedure TForm2.ZGTRClick(Sender: TObject);
begin
ShowMessage('Am in my own created EVENT (procedure) '
+ Sender.ClassName);
end;
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;
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;
Instantiate Controls at Runtime
Cool For Now
Instantiate Controls at Runtime
Clicking The Button
Instantiate Controls at Runtime
Clicking The Label
Instantiate Controls at Runtime
Clicking The Edit
Instantiate Controls at Runtime
• Another Ex: Let’s have the previous example with the following form and modified “ZGTRClick” is like this.
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;
Instantiate Controls at Runtime
• What will happen now when clicking 4 times on any ones of the 3 controls, and then clicking (Show”i”).
Nice Trick :D
• Another Ex:– Let’s see the following Example
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;
Nice Trick :D
Clicking “Create” button two times
After ThatClicking any ones
of created buttons
Array of Objects
Array of Objects
• Declaration
X: Array of String;
M: Array of Array of Integer;
Array of Objects
Array of Objects
• Let’s have the following from design
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;
Array of Objects
• Controls created at Runtime!
Exception Handling
• Let’s have the following Example
Exception Handling
procedure TForm2.Button1Click(Sender: TObject);
var Ptr: ^Integer;
begin
new(Ptr);
Edit1.Text:= IntToStr(Ptr^);
Ptr^:= 3;
Edit1.Text:= IntToStr(Ptr^);
end;
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
Exception Handling
procedure TForm2.Button1Click(Sender: TObject);
var Ptr: ^Integer;
begin
Edit1.Text:= IntToStr(Ptr^);
end;
No Runtime Error
Exception Handlingthe concept
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;
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;
Exception Handling
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
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:@