Solved

Duplicating components runtime

Posted on 2001-06-19
4
206 Views
Last Modified: 2010-04-06
Is there an easy way to duplicate any given component in your app at runtime?

One way is obviously to create a new one and set all the properties manually, but I'm looking for something more flexible.
0
Comment
Question by:Keijo
  • 2
4 Comments
 
LVL 5

Expert Comment

by:TheNeil
ID: 6206099
What about using the Assign method?

The Neil =:)
0
 
LVL 1

Accepted Solution

by:
alx512 earned 100 total points
ID: 6255574
Hi! This code work only for published properties.
Try it.

function CopyComponent(Component: TComponent; NewComponentName: String): TComponent;
var
  Stream: TMemoryStream;
  S: String;
begin
  Result := TComponentClass(Component.ClassType).Create(Component.Owner);
  S := Component.Name;
  Component.Name := NewComponentName;
  Stream := TMemoryStream.Create;
  try
    Stream.WriteComponent(Component);
    Component.Name := S;
    Stream.Seek(0, soFromBeginning);
    Stream.ReadComponent(Result);
  finally
    Stream.Free;
  end;
end;

procedure TForm1.CopyButtonClick(Sender: TObject);
var
  NumberEdit2: TNumberEdit;
begin
  NumberEdit2 := CopyComponent(NumberEdit1, 'NumberEdit2') as TNumberEdit;
  NumberEdit2.Top := NumberEdit2.Top + 50;
  InsertControl(NumberEdit2);
end;
0
 
LVL 1

Expert Comment

by:alx512
ID: 6255580
Try TEdit or other standart VCL component
0
 

Author Comment

by:Keijo
ID: 6259276
Thanks, that works like charm! (had to make some adjustments and publish everything though)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Working with hours 3 58
Reconfigure Delphi Install? 2 52
drawing animated level bar based on numbers 3 98
Multi-layered image in FireMonkey 9 39
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

832 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