delphi l07 controls at runtime p1

31
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 L07 - Creating Controls at Runtime Part 1

Upload: mohammad-shaker

Post on 14-May-2015

287 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Delphi L07 Controls at Runtime P1

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

L07 - Creating Controls at RuntimePart 1

Page 2: Delphi L07 Controls at Runtime P1
Page 3: Delphi L07 Controls at Runtime P1

Creating Controls at Runtime

• Let’s have the following form design

Page 4: Delphi L07 Controls at Runtime P1

var TempShape:TShape;

procedure TForm2.Button1Click(Sender: TObject);

begin

// Now we initialize the shape to use it as a NORMAL

// one

TempShape:= TShape.Create(self);

End;

var i: Integer;

Creating Controls at Runtime

•We declare the “control” like any other variable

Page 5: Delphi L07 Controls at Runtime P1

uses

Windows, Messages, SysUtils, Variants, Classes,

Graphics, Controls, Forms, Dialogs, StdCtrls,

ExtCtrls;

Creating Controls at Runtime

• Compiler Error – Tshape is undeclared type

• So don’t forget to add ExtCtrls.

Page 6: Delphi L07 Controls at Runtime P1

Var TempShape:Tshape;

procedure TForm2.Button1Click(Sender: TObject);

begin

// Now we initialize the shape to use it as a NORMAL

// one

TempShape:= Tshape.Create(self);

End;

Creating Controls at Runtime

•Compile and Run but no shape outputted after writing the following code.

– What’s messing?

Page 7: Delphi L07 Controls at Runtime P1

procedure TForm2.Button1Click(Sender: TObject);

begin

TempShape:= Tshape.Create(self);

TempShape.Parent:= Panel1;

end;

Creating Controls at Runtime

Page 8: Delphi L07 Controls at Runtime P1

procedure TForm2.Button1Click(Sender: TObject);

begin

TempShape:= Tshape.Create(self);

TempShape.Parent:= Panel1;

TempShape.Shape:= stEllipse;

TempShape.Height:= 100;

TempShape.Width:= 50;

TempShape.Left:= 100;

TempShape.Top:= 50;

TempShape.Brush.Color:= 255;

end;

Creating Controls at Runtime

Page 9: Delphi L07 Controls at Runtime P1

Creating Controls at Runtime

Page 10: Delphi L07 Controls at Runtime P1

Creating Controls at Runtime

• Let’s have the following form design

Page 11: Delphi L07 Controls at Runtime P1

procedure TForm2.Button1Click(Sender: TObject);

var i: integer;

ShapeArr: Array [1..5] of TShape;

begin

for i:= 1 to 5 do

Begin

ShapeArr[i]:= TShape.Create(self);

ShapeArr[i].Parent:= Panel1;

ShapeArr[i].Brush.Color:= i*100;

ShapeArr[i].Left:= 100;

ShapeArr[i].Top:= i*40;

End;

end;

Creating Controls at Runtime

Page 12: Delphi L07 Controls at Runtime P1

Creating Controls at Runtime

Page 13: Delphi L07 Controls at Runtime P1
Page 14: Delphi L07 Controls at Runtime P1

Creating Controls at Runtime

Page 15: Delphi L07 Controls at Runtime P1

var

ButtonPtr: ^TButton;

procedure TForm2.Button1Click(Sender: TObject);

var i: integer;

begin

for i:= 1 to StrToInt(Edit1.Text) do

Begin

new(ButtonPtr);

ButtonPtr^:= TButton.Create(self);

ButtonPtr^.Parent:= Panel1;

ButtonPtr^.Left:= 100;

ButtonPtr^.Top:= i*40;

End;

end;

Creating Controls at Runtime

Page 16: Delphi L07 Controls at Runtime P1

Creating Controls at Runtime

Page 17: Delphi L07 Controls at Runtime P1

procedure TForm2.Button1Click(Sender: TObject);

var i: integer;

myButton: ^TButton;

begin

for i:= 1 to StrToInt(Edit1.Text) do

Begin

new(ButtonPtr);

ButtonPtr^:= TButton.Create(self);

ButtonPtr^.Caption:= 'Button' + IntToStr(i);

ButtonPtr^.Parent:= Panel1;

ButtonPtr^.Left:= 100;

ButtonPtr^.Top:= i*40;

End;

end;

Creating Controls at Runtime

Page 18: Delphi L07 Controls at Runtime P1

Creating Controls at Runtime

Page 19: Delphi L07 Controls at Runtime P1

Type TempPtr = ^ TForm;

TempArr = array [1..Cst] of TempPtr;

Var Arr:TempArr;

procedure TForm1.Button1Click(Sender: TObject);

Var i:integer;

begin

for i:=1 to Cst do

Begin

new(Arr[i]); // as he have said we need to New the ptr

Arr[i]^:= TForm.Create(self);

Arr[i]^.Show();// Now in every count a new form will be

// created and showed

End;

End;

Creating Controls at Runtime

Page 20: Delphi L07 Controls at Runtime P1

Creating Controls at Runtime

Page 21: Delphi L07 Controls at Runtime P1

Type TempPtr = ^ TForm;

TempArr = array [1..Cst] of TempPtr;

Var Arr:TempArr;

procedure TForm1.Button1Click(Sender: TObject);

Var i:integer;

begin

for i:=1 to Cst do

Begin

// new(Arr[i]);

Arr[i]^:= TForm.Create(self);

Arr[i]^.Show();// Now in every count a new form will be

// created and showed

End;

End;

Creating Controls at Runtime

Page 22: Delphi L07 Controls at Runtime P1

Creating Controls at Runtime

Page 23: Delphi L07 Controls at Runtime P1

Type

PtrTemp=^Rcd;

Rcd=record

ShapePtr:^Tshape;

Next:PtrTemp;

end;

Var ls,s,Cruiser:PtrTemp;

// we assume that we have the procedure INSERT already

Creating Controls at Runtime

Page 24: Delphi L07 Controls at Runtime P1

// we assume that we have the procedure INSERT already

procedure TForm1.Timer1Timer(Sender: TObject);

Begin

new(s);

new(s^.ShapePtr);

s^.ShapePtr^:= TShape.Create(self);

s^.ShapePtr^.Parent:= Panel1;

s^.ShapePtr^.Show();

s^.ShapePtr^.width:=10;

insert(ls,s);

end;

Creating Controls at Runtime

Page 25: Delphi L07 Controls at Runtime P1

// we assume that we have the procedure INSERT already

procedure TForm1.Timer1Timer(Sender: TObject);

Begin

new(s);

insert(ls,s);

new(s^.ShapePtr);

s^.ShapePtr^:= TShape.Create(self);

s^.ShapePtr^.Parent:= Panel1;

s^.ShapePtr^.Show();

s^.ShapePtr^.width:=10;

End;

Creating Controls at Runtime

Page 26: Delphi L07 Controls at Runtime P1

// we assume that we have the procedure INSERT already

procedure TForm1.Button1Click(Sender: TObject);

Begin

Cruiser:=ls;

While (Cruiser<>nil) do

begin

Cruiser^.ShapePtr:= Shape1;

// Compile Errorincompatible types

Cruiser:= Cruiser^.Next;

end;

end;

Creating Controls at Runtime

Page 27: Delphi L07 Controls at Runtime P1

// we assume that we have the procedure INSERT already

procedure TForm1.Button1Click(Sender: TObject);

Begin

new(Cruiser);

Cruiser:=ls;

While Cruiser<>nil do

begin

Cruiser^.ShapePtr^:= Shape1;

Cruiser:= Cruiser^.Next;

end;

end;

// This will result in all the list (ShapePtr) pointers to be

pointing to the static shape(Shape1).

Creating Controls at Runtime

Page 28: Delphi L07 Controls at Runtime P1

// we assume that we have the procedure INSERT already

procedure TForm1.Button1Click(Sender: TObject);

Begin

new(Cruiser);

Cruiser:=ls;

While Cruiser<>nil do

begin

Cruiser^.ShapePtr^:= Button1;

Cruiser:= Cruiser^.Next;

end;

end;

// Compiler Error. incompatible types

// TButton and Tshape are different from each other

Creating Controls at Runtime

Page 29: Delphi L07 Controls at Runtime P1

// we assume that we have the procedure INSERT already

procedure TForm1.Button1Click(Sender: TObject);

Begin

new(Cruiser);

Cruiser:=ls;

While Cruiser<>nil do

begin

Cruiser^.ShapePtr^.Width:= Cruiser^.ShapePtr^.Width+10;

Cruiser:= Cruiser^.Next;

end;

end;

// This will result in increase of the width of all

// shapes of the list by 10 Cool

Creating Controls at Runtime

Page 30: Delphi L07 Controls at Runtime P1

To be continued..

Page 31: Delphi L07 Controls at Runtime P1

See you!