PC Review


Reply
Thread Tools Rate Thread

How to clone a "Control"

 
 
Rob Stevenson
Guest
Posts: n/a
 
      22nd Feb 2007
Does anyone know how to do this accurately. I really only want to clone the
design-time properties which should make the task easier. I've searched high
and low however and still can't find a problem-free solution. Even (ad-hoc)
solutions posted by MSFT employees have problems. For instance, if you
simply copy all serializable properties, you may eventually receive an
"Object does not match target type" exception. In my case, this occurs when
I encounter (and clone) the "Location" property due to the fact that the
"Site" property was cloned earlier. If I clone "Location" first however
(before "Site"), it works fine. There must be a clean way to do this. Can
anyone offer any insight. Thanks.


 
Reply With Quote
 
 
 
 
RobinS
Guest
Posts: n/a
 
      22nd Feb 2007
You can create the control using the designer, then copy the code and
remove the control, and then you can put the code in the code window and
create the control exactly the same way every time. Would that work for
you?
Robin S.
-------------------------------
"Rob Stevenson" <no_spam@_nospam.com> wrote in message
news:%(E-Mail Removed)...
> Does anyone know how to do this accurately. I really only want to clone
> the design-time properties which should make the task easier. I've
> searched high and low however and still can't find a problem-free
> solution. Even (ad-hoc) solutions posted by MSFT employees have problems.
> For instance, if you simply copy all serializable properties, you may
> eventually receive an "Object does not match target type" exception. In
> my case, this occurs when I encounter (and clone) the "Location" property
> due to the fact that the "Site" property was cloned earlier. If I clone
> "Location" first however (before "Site"), it works fine. There must be a
> clean way to do this. Can anyone offer any insight. Thanks.
>



 
Reply With Quote
 
Rob Stevenson
Guest
Posts: n/a
 
      22nd Feb 2007
> You can create the control using the designer, then copy the code and
> remove the control, and then you can put the code in the code window and
> create the control exactly the same way every time. Would that work for
> you?


Thanks. I wish it were that easy however This is being done at runtime on
a machine where VS isn't even installed. I need to generically clone an
arbitrary control that I'm not familiar with ahead of time. A clone function
taking a "Control" argument and returning the cloned control is what I'm
after.


 
Reply With Quote
 
Morten Wennevik [C# MVP]
Guest
Posts: n/a
 
      22nd Feb 2007
Hi Rob,

There may be better ways, but this may work

private void button1_Click(object sender, EventArgs e)
{
TextBox t = (TextBox)CloneObject(textBox1);
}

private object CloneObject(object o)
{
Type t = o.GetType();
PropertyInfo[] properties = t.GetProperties();

Object p = t.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, o, null);

foreach(PropertyInfo pi in properties)
{
if(pi.CanWrite)
{
pi.SetValue(p, pi.GetValue(o, null), null);
}
}

return p;
}

This code should create a new object of the same type and any writable property will get their values copied.
There may be far better ways though.


On Thu, 22 Feb 2007 02:37:56 +0100, Rob Stevenson <no_spam@_nospam.com> wrote:

> Does anyone know how to do this accurately. I really only want to clone the
> design-time properties which should make the task easier. I've searched high
> and low however and still can't find a problem-free solution. Even (ad-hoc)
> solutions posted by MSFT employees have problems. For instance, if you
> simply copy all serializable properties, you may eventually receive an
> "Object does not match target type" exception. In my case, this occurswhen
> I encounter (and clone) the "Location" property due to the fact that the
> "Site" property was cloned earlier. If I clone "Location" first however
> (before "Site"), it works fine. There must be a clean way to do this. Can
> anyone offer any insight. Thanks.
>
>
>




--
Happy coding!
Morten Wennevik [C# MVP]
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Vista not wotking with "My Computer" or "Control Panel", "Screen Saver" Platebanger Windows Vista General Discussion 6 5th Feb 2008 02:54 PM
How to clone a "Control" Rob Stevenson Microsoft Dot NET Framework Forms 3 22nd Feb 2007 06:03 PM
Hide "Record" "Closed Caption" "Parental Control" indicators while in full screen Jordan ATI Video Cards 0 18th Feb 2005 05:21 PM
How to "clone" or "duplicate" cells & their formulas... Tripp Knightly Microsoft Excel Misc 7 13th May 2004 09:29 PM
"Control Enter" no longer adds "www" or ".com" to site typed into address bar in IE6 GaryB Windows XP Internet Explorer 1 28th Aug 2003 05:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:27 PM.