Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Duplicating components runtime

Posted on 2001-06-19
4
Medium Priority
?
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

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!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

609 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