Solved

copy instance of any control at runtime

Posted on 1997-08-07
8
276 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
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!

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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