Delphi L08 Controls at Runtime P2

57
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

Transcript of Delphi L08 Controls at Runtime P2

Page 1: Delphi L08 Controls at Runtime P2

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
Page 3: Delphi L08 Controls at Runtime P2

Instantiate Controls at Runtime

Page 4: Delphi L08 Controls at Runtime P2

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

Instantiate Controls at Runtime

Page 6: Delphi L08 Controls at Runtime P2

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

Instantiate Controls at Runtime

• Let’s have the following Form

Page 8: Delphi L08 Controls at Runtime P2

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

Instantiate Controls at Runtime

Page 10: Delphi L08 Controls at Runtime P2

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

Instantiate Controls at Runtime

Before Clicking the Dynamic Button

Page 12: Delphi L08 Controls at Runtime P2

Instantiate Controls at Runtime

• Cool, right?

After Clicking the Dynamic Button

Page 13: Delphi L08 Controls at Runtime P2

Instantiate Controls at Runtime

• Another Ex: Let’s Have The following Form

Page 14: Delphi L08 Controls at Runtime P2

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

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

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

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

Instantiate Controls at Runtime

Cool For Now

Page 19: Delphi L08 Controls at Runtime P2

Instantiate Controls at Runtime

Clicking The Button

Page 20: Delphi L08 Controls at Runtime P2

Instantiate Controls at Runtime

Clicking The Label

Page 21: Delphi L08 Controls at Runtime P2

Instantiate Controls at Runtime

Clicking The Edit

Page 22: Delphi L08 Controls at Runtime P2

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

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

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

Nice Trick :D

• Another Ex:– Let’s see the following Example

Page 26: Delphi L08 Controls at Runtime P2

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

Nice Trick :D

Clicking “Create” button two times

After ThatClicking any ones

of created buttons

Page 28: Delphi L08 Controls at Runtime P2

Array of Objects

Page 29: Delphi L08 Controls at Runtime P2

Array of Objects

• Declaration

X: Array of String;

M: Array of Array of Integer;

Page 30: Delphi L08 Controls at Runtime P2

Array of Objects

Page 31: Delphi L08 Controls at Runtime P2

Array of Objects

• Let’s have the following from design

Page 32: Delphi L08 Controls at Runtime P2

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

Array of Objects

• Controls created at Runtime!

Page 34: Delphi L08 Controls at Runtime P2
Page 35: Delphi L08 Controls at Runtime P2

Exception Handling

• Let’s have the following Example

Page 36: Delphi L08 Controls at Runtime P2

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

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

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

Exception Handlingthe concept

Page 40: Delphi L08 Controls at Runtime P2

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

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

Exception Handling

Page 43: Delphi L08 Controls at Runtime P2

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

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

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

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

Delphi Console Application

Page 48: Delphi L08 Controls at Runtime P2

Delphi Console Application

Page 49: Delphi L08 Controls at Runtime P2

Delphi Console Application

Page 50: Delphi L08 Controls at Runtime P2

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

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

Delphi Console Application

Page 53: Delphi L08 Controls at Runtime P2
Page 54: Delphi L08 Controls at Runtime P2

And.. end of course

Page 55: Delphi L08 Controls at Runtime P2

[email protected]://mohammadshakergtr.wordpress.com/

tweet @ZGTRShaker

Page 56: Delphi L08 Controls at Runtime P2
Page 57: Delphi L08 Controls at Runtime P2

See you!