Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2007

Tip: Looking for answers? Try searching our database.

How to clone a "Control"

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rob Stevenson - 22 Feb 2007 01:37 GMT
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.
RobinS - 22 Feb 2007 08:33 GMT
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.
-------------------------------
> 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
[quoted text clipped - 6 lines]
> "Location" first however (before "Site"), it works fine. There must be a
> clean way to do this. Can anyone offer any insight. Thanks.
Rob Stevenson - 22 Feb 2007 12:47 GMT
> 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.
Morten Wennevik [C# MVP] - 22 Feb 2007 18:03 GMT
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.

> 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
[quoted text clipped - 6 lines]
> (before "Site"), it works fine. There must be a clean way to do this. Can
> anyone offer any insight. Thanks.

Signature

Happy coding!
Morten Wennevik [C# MVP]


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.