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 (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...
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...
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
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
That should've been
PropList[i]^.PropType^^.Kind
Sorry.
M.