Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

copy instance of any control at runtime

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
gnom
Asked:
gnom
  • 2
  • 2
  • 2
  • +2
1 Solution
 
StevenBCommented:
 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
 
StevenBCommented:
 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
 
gnomAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
mirek071497Commented:
Can't do this with Assign ?
0
 
bcrotazCommented:
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
 
sperlingCommented:
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
 
bcrotazCommented:
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
 
gnomAuthor Commented:
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

Technology Partners: 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!

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now