Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Cloning a user control

Posted on 2011-10-28
7
Medium Priority
?
706 Views
Last Modified: 2012-05-12
I am creating a user control for an item that I need to be able to duplicate. I need to be able to create a copy/clone of the object when someone selects it. I have been searching, but I have not found a good way of doing this yet. Any help is appreciated.
0
Comment
Question by:aceswildab1
7 Comments
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37047457
Can you provide a little more detail?

Once the object is available you should just need to instantiate another 'copy' of it.
0
 

Author Comment

by:aceswildab1
ID: 37047559
I have an object in a viewer. Once a user clicks on that object, I want to take a copy of that object and recreate the object in another portion of the program. My user control does not have a copy method. If I say newControl = oldControl, it just creates a reference in memory. I need it to be a deep copy/clone so that from one control, I'm creating two.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37047757
You'd need to manually code your own Clone() method that returns a new instance of your UserControl with all the pertinent properties and data copied over.
0
Industry Leaders: 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!

 
LVL 8

Expert Comment

by:Wiesje
ID: 37049487
Exactly as Idle Mind said: you make your own Clone() method which first creates a new instance of the object and then copies over each of the attributes which makes it unique.

I dont believe there is a standard "copy" or "clone" function available - as each object has their own properties.
0
 

Author Comment

by:aceswildab1
ID: 37056448
Thank you for the help. I added a Clone() method to my user control. When I did this and ran it, I got an error that my class was not Serializable, so I added that attribute. When I ran after that, I got the following error:

Type 'System.Windows.Forms.UserControl' in Assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable

Do I need to do something else in order to get it to work or are user controls themselves just not able to be cloned?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 37056480
It needs to be your own "clone" method...something like:
public class MyControl
{
    public MyControl MyClone()
    {
        MyControl mc = new MyControl();
        // manually copy the pertinent details:
        mc.XXX = this.XXX;
        mc.YYY = this.YYY;
        mc.ZZZ = this.ZZZ;
        return mc;
    }
}

Open in new window

0
 

Author Closing Comment

by:aceswildab1
ID: 37056531
That makes sense. Thank you very much for your help. Just ran a couple tests, and they all appear to be working fine.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Loops Section Overview
Suggested Courses

810 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