Control Copy, Different Object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How can I create a second control as a copy of the first, except for the
name, without both controls being the same object?

Thanks.
 
Hi Tom,

You probably will need to serialize your object that you copying (XML or
string for example) and then when pasting you deserialize and get only the
info you need (assuming you are talking of doing it programmatically by
using Clipboard object).

Fitim Skenderi
 
That is not what I am looking for.

Say I have one button on a form and I want to add another button. How do I
create the second button so that the parameters, except name, are the same
without both buttons being the same object?
 
Hi Tom,

Check the code below. It will clone any control.

private void CloneControl(Control originalCtrl, Control newCtrl)
{
PropertyDescriptorCollection origProps =
TypeDescriptor.GetProperties(originalCtrl);
PropertyDescriptorCollection newProps =
TypeDescriptor.GetProperties(newCtrl);

foreach (PropertyDescriptor desc in origProps)
{
if ((desc != null) && (desc.ShouldSerializeValue(originalCtrl)))
{
try
{
newProps[desc.Name].SetValue(newCtrl,
desc.GetValue(originalCtrl));
}
catch { }
}
}
}

hope this helps

Fitim Skenderi
 
Back
Top