Solved

copy instance of any control at runtime

Posted on 1997-08-07
8
265 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

777 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