Solved

copy instance of any control at runtime

Posted on 1997-08-07
8
285 Views
Last Modified: 2010-04-04
i am trying to write a grid where i can put any control on it. that means i have to create an new instance of any control at runtime but with all data that the source control had. the newinstance function does not what i expect.

so i tried it in this way

function CreateControlCopy( tc:TControl ):TControl;
var
  AStream:  TMemoryStream;
  mData:    PTypeData;
begin
  Result := nil;
  AStream := TMemoryStream.Create;
  try
    AStream.WriteComponent(tc);
    mData:=GetTypeData(PTypeInfo(tc.ClassInfo));
    RegisterClass(TPersistentClass(mData^.ClassType));
    AStream.Position:=0;
    Result := TControl(AStream.ReadComponent(NIL));
  finally
    AStream.Free;
  end;
end;

this function is quiet ok but it losts the parent. that means if i have a combobox for example with items defined i get an error. :(

does anyone know how to solve this problem ?

thanks

dejan

0
Comment
Question by:gnom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 4

Expert Comment

by:StevenB
ID: 1340821
 Streamed objects loose their parents. You can reset the parent with a simple assignment (XXXX.Parent := XXXX). What happens when you do this?

  Steven
0
 
LVL 4

Expert Comment

by:StevenB
ID: 1340822
 I'm sorry, I only meant that as a comment (but I checked the wrong box accidentally). Please reject it as an answer, (unless of course it solves your problem (unlikely)) and accept my appologies.

  Steven.
 
0
 

Author Comment

by:gnom
ID: 1340823
well the error occures while reading the stream. that means the function stream.readcomponent tries to load properties that need a parent to be assigned but the function does not recognize that is has no parent in the stream (that may be a bug of delphi).
so i'm sure that i have to solve my problem in another way than to write and read in a stream. and i'm looking for this other way!

dejan
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:mirek071497
ID: 1340824
Can't do this with Assign ?
0
 
LVL 1

Expert Comment

by:bcrotaz
ID: 1340825
You can't use assign, cos you don't know the class type.
You'd only get the properties set that are present in TControl.

0
 
LVL 3

Accepted Solution

by:
sperling earned 100 total points
ID: 1340826
I'll try submitting this as an answer, although it doesn't copy event assignments. Reject it if you need more.

function CopyControl (Ctrl : TControl) : TControl;
var
  CtrlClass : TControlClass;
  CtrlName  : STRING;
  Stream    : TMemoryStream;
begin
  Stream := TMemoryStream.Create;
  CtrlClass := TControlClass(Ctrl.ClassType);
  Result := CtrlClass.Create(Ctrl.Owner);
  CtrlName := Ctrl.Name;
  Ctrl.Name := '';
  Stream.WriteComponent(Ctrl);
  Ctrl.Name := CtrlName;
  Stream.Seek(0, soFromBeginning);
  Result.Parent := Ctrl.Parent;
  Stream.ReadComponent(Result);
  Stream.Free;
end;

Regards,

Erik.
0
 
LVL 1

Expert Comment

by:bcrotaz
ID: 1340827
sperling  - Is that really your name or are you s.perling?
Anyway...

TControlClass looks a bit funky.  Hw should one use it, and how does it know what class to create?
0
 

Author Comment

by:gnom
ID: 1340828
hi sperling i'm really gratefull for this, the function does now that what i expected. this TControlClass is a really funny thing.

thank you for this answer

dejan as gnom
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question