Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to duplicate a Component/Control? [duplicate]

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"?

like image 256
ZigiZ Avatar asked Jul 05 '26 22:07

ZigiZ


1 Answers

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.
like image 159
Uwe Raabe Avatar answered Jul 09 '26 11:07

Uwe Raabe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!