Home » C# ProgrammingRSS

Cloning objects

Hi,

After a lot of searching, I have not been succesful in finding an answer to this question.

In my application I have a group box containing a few controls. At runtime my application may need to duplicate this group box and the objects on it.

I attempted to create an array of the objects I wish to use and copied the properties from the original group to the subsequent objects. However when I run my application instead of seeing copies of the original objects in y form the original objects move from the initial position to where I expect the last copy to appear.

Is it possible to clone groups of controls at runtime, and if so how do I do it?

Thanks in advance

Andrew Ellis

 

4 Answers Found

 

Answer 1

In my application I have a group box containing a few controls. At runtime my application may need to duplicate this group box and the objects on it.

Inherit GroupBox and add your controls.  Instantiate as needed.  Or inherit your group box and instantiate it.
 

Answer 2

Hello,

 

Please take a look at this codeprojects project: How to Clone/Serialize/Copy & Paste a Windows Forms Control .

You should be able to achieve the desired functionality by using this.

 

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

 

Answer 3

The words cloning and copy can be used very misleading by those who are writing about it regarding the Visual Studio Net frameworks.

A copy can be sometimes a real complete copy of all the data

DataTable dt = dataview.ToTable(); (in fact a so called deep copy)

But also a shallow copy or even a clone. 

The later one is something the same as a new type, but then with all kind of properties set.

The shallow copy is the same as the clone, but then with  also all references to the old table set.
(so a change in the shallow table results also in a change in the original table as long as the object already did exist)

If you want to make a deep copy than is mostly your only possibility to serialize and to deserialize the object.

 

Answer 4

Hello,

a) Create a User Control and make a template for your group box and lay the controls however you want.

b) Create a protected copy constructor that takes the same type of this User Control as an argument.

c) Create a method such as DeepClone, or Clone, and let it create instances for of the same type as your User Control .

d) Serialize the data manually, rather than using reflection if possible, and if it's appropriate to your case.

 


Eyal, Regards.

commandbrowser.eyal7.net
 
 
Search for  Cloning objects
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter