Click here to Skip to main content
11,923,958 members (63,733 online)
Click here to Skip to main content
Add your own
alternative version


68 bookmarked

How to Clone/Serialize/Copy & Paste a Windows Forms Control

, 6 Feb 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Clone/Serialize/Copy & Paste a Windows Forms control through serializing its properties.

Sample screenshot


In the .NET environment, the System.Windows.Forms.Control class and all the concrete controls derived from it don’t have clone methods and they are not serializable. So there is no immediate way to clone, serialize, or copy & paste them.

This article presents an all-purpose approach to let you clone, serialize, or copy & paste a Windows Forms control through serializing its properties.


Recently, I was doing some UI programming with C#. One problem I met is that I can not clone or copy & paste a Windows Forms control directly because the System.Windows.Forms.Control class is neither serializable nor does it have a Clone method. After searching the internet and reading James and Nish’s articles, Clipboard Handling with .NET (Part I, Part II), I came up with my own approach to copy & paste a Windows Forms control by serializing its properties.

Using the code

Using the code in the sample application to clone/serialize/copy & paste a Windows Forms control is very simple. The static methods to do serialization and deserialization are wrapped in the ControlFactory class.

Copy & Paste a control

//copy a control to clipboard


//then get it from clipboard and add it to its parent form

Control ctrl = ControlFactory.GetCtrlFromClipBoard();

ctrl.Text = "created by copy&paste";

Clone a control

//clone a control directly and then add it to its parent form

Control ctrl = ControlFactory.CloneCtrl(this.comboBox1);

ctrl.Text = "created by clone";

Implementation Details

When you clone/paste a control, the ControlFactory creates a new control through reflection, with the class name and namespace (partialName) passed to it.

Assembly controlAsm = Assembly.LoadWithPartialName(partialName);
Type controlType = controlAsm.GetType(partialName + "." + ctrlName);
ctrl = (Control)Activator.CreateInstance(controlType);
return ctrl;

If the new control is successfully created, ControlFactory then sets the properties of the new control with the property values it had retrieved from the original control.

public static void SetControlProperties(Control ctrl,Hashtable propertyList)
       PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(ctrl);

       foreach (PropertyDescriptor myProperty in properties)
                    Object obj = propertyList[myProperty.Name];

In .NET clipboard programming, to create a custom format that uses a class, you must make the class serializable, that is it needs to have the Serializable attribute applied to it. CBFormCtrl is such a custom data format, it also uses a hash table to store the serializable properties.

public class CBFormCtrl//clipboard form control
       private static DataFormats.Format format;
       private string ctrlName;
       private string partialName;
       private Hashtable propertyList = new Hashtable();

Points of Interest

I’ve tested this approach with almost all the Windows Forms controls, it works fine with most of them. Unfortunately, when I copy & paste a ListView/ListBox/CheckedListBox or a TreeView, their item data will be lost, that is because the “Items” property of a ListView/ListBox/CheckedListBox and the “Node” property of a TreeView are not serializable.

To completely copy & paste these controls with their item data, you need to do some extra handling of the “Items” property or the “Node” property. As a reference, you can look at Tom John’s article to see how to fully serialize a TreeView control.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

China China

You may also be interested in...

Comments and Discussions

QuestionThanks Pin
violinist_tr31-Dec-14 5:53
memberviolinist_tr31-Dec-14 5:53 
QuestionProps to Developer Pin
Member 1067105618-May-14 12:44
memberMember 1067105618-May-14 12:44 
QuestionMissing executable Pin
Megabyte2618-Sep-13 13:22
memberMegabyte2618-Sep-13 13:22 
QuestionHow ican clone a system Window? Pin
mesiasrojo12-Aug-11 1:42
membermesiasrojo12-Aug-11 1:42 
QuestionActuall serialization is not working! Pin
Fadik15-Jul-09 4:07
memberFadik15-Jul-09 4:07 
QuestionHow I can save/read controls to file? Pin
drinkmaker21-Feb-09 12:43
memberdrinkmaker21-Feb-09 12:43 
QuestionHow about the TabControl Pin
Sean_Miller19-Feb-09 10:39
memberSean_Miller19-Feb-09 10:39 
AnswerRe: How about the TabControl [modified] Pin
pataguillon21-Jul-09 3:48
memberpataguillon21-Jul-09 3:48 
GeneralRe: How about the TabControl [modified] Pin
Member 786104014-Apr-14 23:50
memberMember 786104014-Apr-14 23:50 
GeneralThank you very much it helped me a lot Pin
SanShark6-Feb-09 1:20
memberSanShark6-Feb-09 1:20 
GeneralThank you Pin
hlmc29-Apr-08 21:24
memberhlmc29-Apr-08 21:24 
QuestionCopying data Pin
Ajmeri28-Feb-08 9:26
memberAjmeri28-Feb-08 9:26 
GeneralJust Great Pin
shapez22-Oct-07 3:58
membershapez22-Oct-07 3:58 
GeneralAssembly.LoadWithPartialName(string)' is obsolete Pin
gajatko26-Aug-07 9:03
membergajatko26-Aug-07 9:03 
GeneralRe: Assembly.LoadWithPartialName(string)' is obsolete Pin
Member 1067105622-May-14 16:45
memberMember 1067105622-May-14 16:45 
GeneralSerializing Widows.Forms in C# Pin
SakthiSurya29-Apr-07 21:24
memberSakthiSurya29-Apr-07 21:24 
GeneralA Suggestion Pin
fonk6665-Feb-07 11:27
memberfonk6665-Feb-07 11:27 
QuestionAbout Events ? Pin
whithix19-Sep-06 2:02
memberwhithix19-Sep-06 2:02 
AnswerRe: About Events ? Pin
lxwde19-Sep-06 21:23
memberlxwde19-Sep-06 21:23 
QuestionUsing this code with personalized controls. Pin
DoXiGen29-Aug-06 6:10
memberDoXiGen29-Aug-06 6:10 
AnswerRe: Using this code with personalized controls. Pin
lxwde29-Aug-06 17:05
memberlxwde29-Aug-06 17:05 
AnswerRe: Using this code with personalized controls. Pin
Hotcut17-Oct-06 0:48
memberHotcut17-Oct-06 0:48 
GeneralSerialization thoughts ... Pin
BillWoodruff13-Feb-06 18:46
memberBillWoodruff13-Feb-06 18:46 
Nice work. I've wondered if there are any facilities in the FrameWork that could be exploited for using reflection and whatever-other techniques at run-time to parse all the properties of a control and clone/serialize.

Would like to know more about what's under the hood in the IDE when you use it to copy a control at design-time.

best, Bill

"The greater the social and cultural distances between people, the more magical the light that can spring from their contact." Milan Kundera in Testaments Trahis
GeneralRe: Serialization thoughts ... Pin
lxwde14-Feb-06 3:24
memberlxwde14-Feb-06 3:24 
GeneralRe: Serialization thoughts ... Pin
Nigel Savidge2-Mar-06 8:17
memberNigel Savidge2-Mar-06 8:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151125.3 | Last Updated 6 Feb 2006
Article Copyright 2006 by lxwde
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid