Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Copy/Clone component

98 views
Skip to first unread message

Lucas

unread,
May 27, 2002, 6:47:37 PM5/27/02
to
How do you copy/clone a component at runtime?

Ex:

Button1 has a caption, events etc..

then I want to copy it..

Button2.create // clone button1, exact same button but works independently.


Lucas


Paul Nicholls

unread,
May 27, 2002, 9:13:51 PM5/27/02
to
If a lot of classes have an assign method that "clones" from an equivalent
class...look it up :-)

--
Paul Nicholls (Delphi 5 Professional)
"Life is like a road - occasionally you run into potholes or get blowouts." -
Paul Nicholls

HomePage: www.southcom.com.au/~phantom
Email: paul_f_...@antispam.hotmail.com
(Remove "antispam." to reply)
"Lucas" <Luca...@hotmail.com> wrote in message news:3cf2b7ec$1_1@dnews...

Paul Nicholls

unread,
May 27, 2002, 9:32:47 PM5/27/02
to
<g> that should have been:

A lot of classes have an assign method that "clones" from an equivalent
class...look it up :-)

and not

If a lot of classes have an assign method that "clones" from an equivalent
class...look it up :-)


Mental note to self => "get more sleep...",

:)

Paul Nicholls (Delphi 5 Professional)
"Life is like a road - occasionally you run into potholes or get blowouts." -
Paul Nicholls

HomePage: www.southcom.com.au/~phantom
Email: paul_f_...@antispam.hotmail.com
(Remove "antispam." to reply)

"Paul Nicholls" <paul_f_...@antispam.hotmail.com> wrote in message
news:3cf2d9e3$1_2@dnews...

Martin

unread,
May 28, 2002, 2:31:01 AM5/28/02
to
In article <3cf2b7ec$1_1@dnews>, Luca...@hotmail.com says...

Something like this.
Though you'll have to handle property 'Name' for itself, otherwise you'll
get 'Form1 already have a component named '-errors.
Note also that I'm passing strings, you might want to check the type of
the property (PropList[l_IT_i]^.PropType^.Kind) and do different things
there. In fact, this will probably fail pretty badly if the property is
another class. And I'm unsure what would happen with events here.
But, something must always be left as an exercise for the reader...

M.

uses
TypInfo;

function Clone(const Comp : TComponent) : TComponent;
var
PropList : TPropList;
Count : integer;
i : integer;
Name : string;
Value : string;
Compclass : TComponentClass;
begin
Compclass := TComponentClass(Comp.ClassType);
Result := Compclass.Create(Comp.Owner);
Count := GetTypeData(Comp.ClassInfo)^.PropCount;
GetPropInfos(Comp.ClassInfo, @PropList);
for i := 0 to Count - 1 do begin
Name := PropList[i]^.Name;
if GetPropInfo(Result, Name)^.SetProc <> nil then begin
// Writable
Value := GetPropValue(Comp, Name, True);
SetPropValue(Result, Name, Value);
end; // GetPropInfo(Result, Name)^.SetProc <> nil
end; // for i := 0 to Count - 1
end; // Clone

serge gubenko

unread,
May 28, 2002, 5:54:05 AM5/28/02
to
"Lucas" <Luca...@hotmail.com> wrote in message news:3cf2b7ec$1_1@dnews...

Hi, Lucas

You can use streams to clone any component you want. Force it to store
properties to the stream and then read them to the new copy. Function listed
below acts exactly as described.
____________________________________
function CloneComponent(AAncestor: TComponent): TComponent;
var
XMemoryStream: TMemoryStream;
XTempName: string;
begin
Result:=nil;
if not Assigned(AAncestor) then exit;

XMemoryStream:=TMemoryStream.Create;
try
XTempName:=AAncestor.Name;
AAncestor.Name:='clone_' + XTempName;
XMemoryStream.WriteComponent(AAncestor);
AAncestor.Name:=XTempName;
XMemoryStream.Position:=0;
Result:=TComponentClass(AAncestor.ClassType).Create(AAncestor.Owner);
if AAncestor is TControl
then TControl(Result).Parent:=TControl(AAncestor).Parent;
XMemoryStream.ReadComponent(Result);
finally
XMemoryStream.Free;
end;
end;
__________________________________
Best regards, Serge Gubenko


Martin

unread,
May 28, 2002, 10:00:37 AM5/28/02
to
In article <MPG.175d4293...@forums.borland.com>,
LLOQPT...@spammotel.com says...
> PropList[l_IT_i]^.PropType^.Kind

That should've been
PropList[i]^.PropType^^.Kind
Sorry.

M.

0 new messages