3

Possible Duplicate:
Duplicating components at Run-Time

I have a TMyControl (Control1) with it's own properties/events.
How can I create a duplicate instanse Control2 that will have exactly the same properties/events?


To be more specific, I would like to clone an existing TADODataSet with streaming fields (and some events):

object ADODataSet1: TADODataSet
  Connection = ADOConnection1
  CursorType = ctStatic
  AfterOpen = ADODataSet1AfterOpen
  CommandText = 'select top 10  * from Polls'
  Parameters = <>
  Left = 224
  Top = 40
  object ADODataSet1PollID: TGuidField
    FieldName = 'PollID'
    FixedChar = True
    Size = 38
  end
  object ADODataSet1Title: TWideStringField
    FieldName = 'Title'
    Size = 255
  end
  object ADODataSet1Description: TWideStringField
    FieldName = 'Description'
    Size = 4000
  end
  object ADODataSet1PollType: TIntegerField
    FieldName = 'PollType'
  end
end

Since you closed this question, will you consider a duplicate if I ask a new question "How to duplicate a TADODataSet with Persistent fields"?

5
  • 2
    You can't in general. If Control1 has a non-empty Name property, then no other sibling is allowed to have the same value for that property. Feb 27, 2012 at 17:13
  • 2
    @RobKennedy, It is possible to temporary set Name to empty string and restore it.
    – ZigiZ
    Feb 27, 2012 at 17:16
  • TDataSet isn't a TControl.
    – NGLN
    Feb 27, 2012 at 18:26
  • Look at TClientDataSet which has cloning capabilities.
    – NGLN
    Feb 27, 2012 at 18:27
  • @NGLN, The title says "Component/Control". I don't think there should be a difference. WriteComponent, ReadComponent works. but the events are not duplicated, nor the streaming fields.
    – ZigiZ
    Feb 27, 2012 at 18:33

2 Answers 2

8

The following code might give some direction:

unit Unit130;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Vcl.StdCtrls;

type
  TForm130 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    edit2: TEdit;
  public
  end;

var
  Form130: TForm130;

implementation

{$R *.dfm}

procedure TForm130.Button1Click(Sender: TObject);
var
  component: TComponent;
  stream: TMemoryStream;
begin
  RegisterClass(TEdit);
  stream := TMemoryStream.Create;
  try
    stream.WriteComponent(edit1);
    stream.Position := 0;
    component := stream.ReadComponent(nil);
    edit2 := component as TEdit;

    { this is necessary to make the following InsertComponent work }
    edit2.Name := 'Edit2';
    InsertComponent(edit2);

    edit2.Parent := Self;
    edit2.Top := edit2.Top + 30;
  finally
    stream.Free;
  end;
end;

end.
4
  • Checking into it. Should I call UnRegisterClass before Button1Click exits? p.s: strangely I do not get an exception if I click the button more than once... (Edit2 is created with the same name more than once?)
    – ZigiZ
    Feb 27, 2012 at 17:54
  • 1
    Indeed! It seems quite possible to add a component with the same name as long as the newly added component doesn't have the container as its owner.
    – Uwe Raabe
    Feb 27, 2012 at 21:06
  • Couldn't help but to notice Form130... Jan 26, 2015 at 22:20
  • 1
    @JerryDodge, indeed! Over the time the Delphi projects folder gets somewhat crowded - until you get a new system. BTW, the dpr was named Project124...
    – Uwe Raabe
    Jan 26, 2015 at 23:38
4

Copy all property values.

There is no default mechanism for this. And there is a reason for it. The problem with your request is that you really do not want all settings duplicated. For instance: should it appear at the same parent at the same location and with the same dimensions? And even if yes: which of both should be on top?

Not the answer you're looking for? Browse other questions tagged or ask your own question.