Solved

copy instance of any control at runtime

Posted on 1997-08-07
8
255 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
 
LVL 3

Expert Comment

by:mirek071497
ID: 1340824
Can't do this with Assign ?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now