Sunncity.com - Developer & I.T. Zone

 
Sunncity Web

Technology Menu

Developer Center
Delphi Online Tutorial
JAVA Online Tutorial
The Code Project
XML Code Library
FREE Download
Our Free Components
Our Free Software
Free Components
Delphi & Kylix Training
SMTC Training Course
About Delphi EXAM

Search Component


Search Software(s)


by oneNetwork

 

Tutorial - CREATING OBJECT ON THE FLY

7 Steps in Dymanic Component Creation (2)

1 * Declare an instance of a specified component type.(2/33)
2 * Called inherited create constructor to return a reference to that component (2/33-2)
3 * Assigned Preliminary property and default value for the object.(2/34)
4 * Assigned Parent to control to provide its visibility.(2/34-2)
5 * Make Control visible (require for Delphi 1 only)(2/35)
6 * Assigned Event Handler to the control by writing code. (2/35-2)
7 * Detach Event Handler and Destruct the Object.(2/36)
InDirect Inheritance (2/36-2)
Dynamic Form in Delphi (2/37)

 


1 * Declare an instance of a specified component type.(2/33)
This is another 7 steps (additional to standard 10 rules for OOP you should already know).
You need to declare specific Instance to be use as references of CLASS which you want to create, and call Uses at that Class too.

Var
.......MyButton : TBitBin;

Back to TOP

2 * Called inherited create constructor to return a reference to that component (2/33-2)
Call Method "Create" in order to put Component up into memory which then will return to us "Reference" as an "Instance" before put it to use.

Command "Create" is similar to function which return address in memory of that component or object (think of mobile phone number pinpoint where you are, but mobile phone number is not the physical phone itself).

The difference between creating a normal class and your own class is that you need to indicate owner by using command "Create" to indicate how you want to destroy your component.

.......MyButton : TBitBin.Create(Self);
.......MyButton : TBitBin.Create(Application);
.......MyButton : TBitBin.Create(Nil);

Create(Self) indicate that when this component or CLASS is destroy, everything which comes with this component will be destroy too.
Create(Application) indicate that this application or form is the OWNER of MyButton, if application or form is destroy or end program then destroy this component too.
Create (Nil) indicate NO owner, if Nil then programmer will decide how to destroy his component.

Back to TOP

3 * Assigned Preliminary property and default value for the object.(2/34)
You can set default value for your component property (top, left, width, height) or simply use command "SetBounds". Some component can be non-visual which can not be seen when RUN program, but can be seen when designing. You should not indicate setting WHERE, but just it's property.

.......MyButton.Caption := '&Confirm';
.......MyButton.Top :=5;
.......MyButton.Left :=5;
.......MyButton.Width :=80;
.......MyButton.Height :=25;

Or write in short.

.......MyButton.Caption := '&Confirm';
.......MyButton.SetBounds(5,5,80,25);

Back to TOP

4 * Assigned Parent to control to provide its visibility.(2/34-2)
Our component might have to attach (cling) to some Parent control and parent control have to be a type of TWinControl such as TForm, TPanel, TScrollBox.

.......MyButton.Parent :=Self ;
....................Or
.......MyButton.Parent :=Form1; <provided that form1 exists>

Note : Indicate Class we are writing is Parent of MyButton
OR Form1 is parent control of MyButton

Or use Method by using command "InsertControl" which is in TWinControl, so that we can insert our component in TWinControl at ease.

.......Self.InsertControl (MyButton);
.......Form1.InsertControl (MyButton);

PROBLEM : In case of difficulty on setting TWinControl for be a parent control then adding this
ControlStyle : = ControlStyle + [CsAcceptControls] ;
to enable that component to be a parent control, then you can try to set TWinControl to be a parent control again as state method above.

Back to TOP

5 * Make Control visible (require for Delphi 1 only). (2/35)

.......MyButton.Visible := True;

Back to TOP

6 * Assigned Event Handler to the control by writing code. (2/35-2)
If we drop a component on form when designing we can just simply press F11 to go to Object inspector, but when you are designing component, you will need to write a code to assigned how you want to handle events or actions such as onClick, OnMouseMove and etc.

.......MyButton.OnClick := DoSomeOnClickMethod;
.......MyButton.OnMouseMove := MouseMoveRoutine;
.......MyButton.OnDblClick :=MyDoubleClickMethodRoutineForThisButton;

Back to TOP

7 * Detach Event Handler and Destruct the Object.(2/36)

.......MyButton.OnClick := Nil;
.......MyButton.OnMouseMove := Nil;
.......MyButton.OnDblClick := Nil;

.......MyButton.Free;
.......MyButton.Destroy;

You need to destroy your object and event to give back space to memory or else your program will keep on getting fatter. In case of creating component on top of another component, it's better to use this rule to destroy the object. The best way is to do it is at Destructor of that component's parent.

If you indicate owner to that component, you DO NOT need to FREE it in order to destroy References (instance), because Owner of the component will automatically destroy your component for you.

You might have many owner for your component such as 1 Owner responsible for destroy the component and 1 Owner responsible in showing the component. IF Parent and OWNER is the same object, you can simply write

 

.......MyButton.Parent := TWinControl(Owner);

Back to TOP

* InDirect Inheritance (2/36-2)
Writing code to assigned event handler is not quite convenient and mostly be applied to those experienced programmer with Delphi. It's a wise idea to create a CLASS then override, or modify it's method to your own preferred behavior.

See sample below

Type
.......TMyButton = Class(TBitBtn)
.......Public

.......Constructor CreatedMeToo(AOwner:TComponent;X,Y:Integer;CapStr;String);Override;
.......Procedure Click;Override;
.......Procedure MouseMove;Override;
.......Procedure MouseDown;Override;
.......Procedure WM_MouseDown(Var Msg:TMessage) Message WM_MouseDown;

End;

Procedure
TMyButton.CreateMeToo(AOwner:TComponent;X;Y:Integer;CapStr:String);
.....Begin
.......Left := X; Top :=Y;
.......Width:=50; Height:=25;
.......Caption := CapStr;
.......inherited Create(AOwner);
End;

Note : from above we created Class TMyButton from Class TBitBtn ....using method "Indirect", so we can add, modify Class the way we like.

Instead of creating ordinary TBitBtn, we can do this.

.......MyButton: =TMyButton.CreateMeToo(Self,5,5'&Confirm');

Now event MouseMove and MouseDown and other Events are already here.

Back to TOP

* Dynamic Form in Delphi (2/37)

One form is approximately 28-40 K (sound small but actually one program is not that small). Form is upload in to memory and therefore slow down your program. Dynamic form allow you to maximize your program loading effectively and fast.

Create form when need. (PRE-PLAN form creation)

Var
.......Form1 : TForm1;
.......Form1 : = TForm1.Create (Nil);
.......Form1.ShowModal;
.......Form1.Free;

Note : from above if Form is Create and have no owner (Nil), then show form "Showmodal" and when FINISH get rid of this form.

Create form when need. (NOT so PRE-PLAN form creation)

Var
.......Form1 : TForm1 = Nil;
.......If Form1 = Nil then
..........Form1:= TForm1.Create (Application);
..........Form1.Show;

Note : from above Form is Create with no owner (Nil) and then assign owner to be Application, so get rid of this form when EXIT this program.

Back to TOP

 


Support this site BUY from Sunncity Gift Store