BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Component copying

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.components.usage
View previous topic :: View next topic  
Author Message
Mattia Massimo
Guest





PostPosted: Sat Jun 26, 2004 5:15 pm    Post subject: Component copying Reply with quote



Is there any simple and reliable method to copy components even using
streams ?

Fo example: I have a panel with some controls into ad i want to make an
exact copy of this panel, another instance of TPanl containing same controls
of the original. I have used streams and writecomponent and read component
but I get exception error component x has no parent. Creating the components
instances manually and then reading properties on the streams (such as
passing the instance to root param of tstream's readcomponent method) fail
to load some properties such as ttreeview items and tlistview columns even I
used the exact class type of the original.


Back to top
Mattia Massimo
Guest





PostPosted: Sat Jun 26, 2004 5:26 pm    Post subject: Re: Component copying Reply with quote



I will insert the source code for example:

Begin
NewTab:=TTabSheet.Create(ChatPC);
NewTab.Caption:=aRoom;
NewTab.PageControl:=ChatPC;
NewPanel:=TPanel.Create(Self);
NewPanel.Align:=alClient;
NewTab.InsertControl(NewPanel);
S:=TMemoryStream.Create;
For C:=0 To ChannelLay.ControlCount-1
Do
Begin
RegisterClass
(TPersistentClass(ChannelLay.Controls[C].ClassType));
S.WriteComponent(ChannelLay.Controls[C]);
End;
S.Seek(0,0);
For C:=0 To ChannelLay.ControlCount-1
Do
Begin

Control:=TControlClass(ChannelLay.Controls[C].ClassType).Create(Nil);
NewPanel.InsertControl(Control);
S.ReadComponent(Control);
End;
S.Free;
End

Here Channellay is a TPanel to clone, S is a TStream, NewPanel is a TPanel
where ChannelLay should copy into.

"Mattia Massimo" <dhalsimmax (AT) tin (DOT) it> ha scritto nel messaggio
news:KaiDc.535079$rM4.22365955 (AT) news4 (DOT) tin.it...
Quote:
Is there any simple and reliable method to copy components even using
streams ?

Fo example: I have a panel with some controls into ad i want to make an
exact copy of this panel, another instance of TPanl containing same
controls
of the original. I have used streams and writecomponent and read component
but I get exception error component x has no parent. Creating the
components
instances manually and then reading properties on the streams (such as
passing the instance to root param of tstream's readcomponent method) fail
to load some properties such as ttreeview items and tlistview columns even
I
used the exact class type of the original.





Back to top
Mattia Massimo
Guest





PostPosted: Sat Jun 26, 2004 6:00 pm    Post subject: Re: Component copying Reply with quote



New code much more simple but still the ttreeview items and tlistviuew
colums not saved:

Begin
NewTab:=TTabSheet.Create(ChatPC);
NewTab.Caption:=aRoom;
NewTab.PageControl:=ChatPC;
NewPanel:=TPanel.Create(Self);
NewPanel.Align:=alClient;
NewTab.InsertControl(NewPanel);
S:=TMemoryStream.Create;
ChannelLay.Tag:=ChannelLAy.Tag+1;
For C:=0 To ChannelLay.ControlCount-1
Do
Begin
RegisterClass
(TPersistentClass(ChannelLay.Controls[C].ClassType));
S.Seek(0,soFromBeginning);
S.WriteComponent(ChannelLay.Controls[C]);
ChannelLay.Controls[C].Name:=Format('%s_%u',[Copy
(ChannelLay.Controls[C].Name,0,Pos('_',ChannelLay.Controls[C].Name)),Channel
Lay.Tag]);
Control:=TControlClass(FindClass
(ChannelLay.Controls[C].ClassName)).Create(Self);
NewPanel.InsertControl(Control);
S.Seek(0,soFromBeginning);
S.ReadComponent(Control);
End;
S.Free;
End


Note to avoid name conficts i'm usign the tag property to form the new
copied control name.
Anyone could help me pls ?

"Mattia Massimo" <dhalsimmax (AT) tin (DOT) it> ha scritto nel messaggio
news:mliDc.535133$rM4.22368141 (AT) news4 (DOT) tin.it...


Back to top
Richard C Haven
Guest





PostPosted: Sun Jun 27, 2004 11:05 pm    Post subject: Re: Component copying Reply with quote

Maybe this will help. It will NOT copy TreeView nodes or StringGrid
cells, but you can add code for that.

Cheers

procedure AssignAllProperties(Source, Target : TPersistent);
var
ThisProp : PPropInfo;
PropInfos : TPropList;
Counter : System.Integer;
begin
TypInfo.GetPropInfos(Source.ClassInfo, @PropInfos);
for Counter := 0 to GetTypeData(Source.ClassInfo)^.PropCount - 1 do
begin
ThisProp := PropInfos[Counter];
if (ThisProp.Name = 'Name') and (ThisProp.PropType^.Kind =
tkString) then
SetStrProp(Target, ThisProp, GetStrProp(Source, ThisProp) +
'Clone')
else
begin
case ThisProp.PropType^.Kind of
tkInteger, tkChar, tkEnumeration, tkSet, tkClass, tkWChar :
SetOrdProp(Target, ThisProp, GetOrdProp(Source,
ThisProp));
tkFloat : SetFloatProp(Target, ThisProp,
GetFloatProp(Source, ThisProp));
tkString, tkLString, tkWString :
SetStrProp(Target, ThisProp, GetStrProp(Source,
ThisProp));

tkVariant : SetVariantProp(Target, ThisProp,
GetVariantProp(Source, ThisProp));
tkMethod : SetMethodProp(Target, ThisProp,
GetMethodProp(Source, ThisProp));
tkInt64 : SetInt64Prop(Target, ThisProp,
GetInt64Prop(Source, ThisProp));
end;
end;
end;
end;

procedure ClonePersistent(Source : TPersistent; var Target : TPersistent);
var
ComponentClass : TComponentClass;
begin
if Target = nil then
begin
if Source is TComponent then
begin
ComponentClass := TComponentClass(Source.ClassType);
Target := ComponentClass.Create(TComponent(Source).Owner);
TComponent(Target).Name := TComponent(Source).Name + 'Clone';
end
else
Target := Source.Create;
end;

if Source is TControl then
TControl(Target).Parent := TControl(Source).Parent;

AssignAllProperties(Source, Target);
end;


Mattia Massimo wrote:

Quote:
Is there any simple and reliable method to copy components even using
streams ?

Fo example: I have a panel with some controls into ad i want to make an
exact copy of this panel, another instance of TPanl containing same controls
of the original. I have used streams and writecomponent and read component
but I get exception error component x has no parent. Creating the components
instances manually and then reading properties on the streams (such as
passing the instance to root param of tstream's readcomponent method) fail
to load some properties such as ttreeview items and tlistview columns even I
used the exact class type of the original.



Back to top
Mattia Massimo
Guest





PostPosted: Mon Jun 28, 2004 7:30 am    Post subject: Re: Component copying Reply with quote

Yes this is fine, but is there also a method just like the copy and paste @
designtime in delphi ide ? This method should copy everithing. I think
should be something using streaming capabilities.

"Richard C Haven" <richard.spalmless (AT) santacruzsoftware (DOT) com> ha scritto nel
messaggio news:5pIDc.78793$Fr6.57865 (AT) newssvr29 (DOT) news.prodigy.com...
Quote:
Maybe this will help. It will NOT copy TreeView nodes or StringGrid
cells, but you can add code for that.

Cheers

procedure AssignAllProperties(Source, Target : TPersistent);
var
ThisProp : PPropInfo;
PropInfos : TPropList;
Counter : System.Integer;
begin
TypInfo.GetPropInfos(Source.ClassInfo, @PropInfos);
for Counter := 0 to GetTypeData(Source.ClassInfo)^.PropCount - 1 do
begin
ThisProp := PropInfos[Counter];
if (ThisProp.Name = 'Name') and (ThisProp.PropType^.Kind =
tkString) then
SetStrProp(Target, ThisProp, GetStrProp(Source, ThisProp) +
'Clone')
else
begin
case ThisProp.PropType^.Kind of
tkInteger, tkChar, tkEnumeration, tkSet, tkClass, tkWChar
:
SetOrdProp(Target, ThisProp, GetOrdProp(Source,
ThisProp));
tkFloat : SetFloatProp(Target, ThisProp,
GetFloatProp(Source, ThisProp));
tkString, tkLString, tkWString :
SetStrProp(Target, ThisProp, GetStrProp(Source,
ThisProp));

tkVariant : SetVariantProp(Target, ThisProp,
GetVariantProp(Source, ThisProp));
tkMethod : SetMethodProp(Target, ThisProp,
GetMethodProp(Source, ThisProp));
tkInt64 : SetInt64Prop(Target, ThisProp,
GetInt64Prop(Source, ThisProp));
end;
end;
end;
end;

procedure ClonePersistent(Source : TPersistent; var Target : TPersistent);
var
ComponentClass : TComponentClass;
begin
if Target = nil then
begin
if Source is TComponent then
begin
ComponentClass := TComponentClass(Source.ClassType);
Target := ComponentClass.Create(TComponent(Source).Owner);
TComponent(Target).Name := TComponent(Source).Name + 'Clone';
end
else
Target := Source.Create;
end;

if Source is TControl then
TControl(Target).Parent := TControl(Source).Parent;

AssignAllProperties(Source, Target);
end;


Mattia Massimo wrote:

Is there any simple and reliable method to copy components even using
streams ?

Fo example: I have a panel with some controls into ad i want to make an
exact copy of this panel, another instance of TPanl containing same
controls
of the original. I have used streams and writecomponent and read
component
but I get exception error component x has no parent. Creating the
components
instances manually and then reading properties on the streams (such as
passing the instance to root param of tstream's readcomponent method)
fail
to load some properties such as ttreeview items and tlistview columns
even I
used the exact class type of the original.





Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.components.usage All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.