title: How to Clone a Windows Forms Controls even with non-Serializable properties?
body: <p>How to Clone or Serialize a Windows Forms Control?<br>
  When I am trying to Clone windows forms controls using this code "CloneControl(Control ct1)", it allows me to duplicate controls with some Serializable properties, not with all properties.              </p>
  
  <pre><code>public Form1()
  {
      InitializeComponent();
          Columns = new DataGridViewTextBoxColumn[2];
          for (int i = 0; i &lt; 2; i++)
          {
              Columns[i] = new System.Windows.Forms.DataGridViewTextBoxColumn();
              // 
              // Columns[i]
              // 
              Columns[i].HeaderText = "j" + (i + 1);
              Columns[i].Name = "Column" + (i + 1);
              Columns[i].Width = 50;
          }
          dataGridView1 = new System.Windows.Forms.DataGridView();
          dataGridView1.Name = "dataGridView1";
          dataGridView1.Location = new System.Drawing.Point(100, 100);
          dataGridView1.RowHeadersWidth = 50;
          dataGridView1.RowTemplate.Height = 25;
          dataGridView1.Size = new System.Drawing.Size(55 + 50 * 2, 25 + dataGridView1.RowTemplate.Height * 2);
          dataGridView1.Anchor = System.Windows.Forms.AnchorStyles.None;
          dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
          dataGridView1.Columns.AddRange(Columns);
          dataGridView1.TabIndex = 3;
          dataGridView1.AllowUserToAddRows = false;
          dataGridView1.Rows.Add();
          dataGridView1.Rows.Add();
          dataGridView1.Rows[0].HeaderCell.Value = "i" + 1;
          dataGridView1.Rows[1].HeaderCell.Value = "i" + 2;
          dataGridView1.Rows[0].Cells[0].Value = "value1";
          Controls.Add(dataGridView1);
  
          Control cloned1 = CloneControl(dataGridView1); 
          cloned1.SetBounds(cloned1.Location.X, cloned1.Location.Y + 300, cloned1.Width, ct1.Height);
          Controls.Add(cloned1);
          cloned1.Show();
  }    
  
  public Control CloneControl(Control ct1)
  {
      Hashtable PropertyList = new Hashtable();
      PropertyDescriptorCollection Properties = TypeDescriptor.GetProperties(ct1);
      Assembly controlAsm = Assembly.LoadWithPartialName(ct1.GetType().Namespace);
      Type controlType = controlAsm.GetType(ct1.GetType().Namespace + "." + ct1.GetType().Name);
      Control cloned1 = (Control)Activator.CreateInstance(controlType);
      foreach (PropertyDescriptor pr1 in Properties)
      {
          if (pr1.PropertyType.IsSerializable)
          {
              PropertyList.Add(pr1.Name, pr1.GetValue(ct1));
          }
          if (PropertyList.Contains(pr1.Name))
          {
              try
              {
                   Object obj = PropertyList[pr1.Name];
                   pr1.SetValue(cloned1, obj);
              }
              catch (Exception ex)
              {
  
              }
          }
      }
      return ct2;
  }
  </code></pre>
  
  <p>If you run the code... the you will get </p>
  
  <p><img src="https://i.stack.imgur.com/xwh12.png" alt="enter image description here"></p>
  
  <p>As you can see in the main method I create a clone of dataGridView1, which has a few properties.<br>
  And actually each cell value is null in a cloned dataGridView.
  Also size of a columns are not cloned!</p>
  
  <p>You may have a question: if Visual Studio or SharpDeveloper as IDE which is written in C# can handle this problem, then it might be possible to write that kind of code! Right?<br>
  In Visual Studio When you are trying drag and drop controls, or copy and paste controls, it not only duplicates that controls with all properties (including Serializable or non-Serializable) but also it changes the name of control itself from "dataGridView1" to "dataGridView2" as well as in SharpDeveloper!</p>
  
  <p>What should I do?<br>
  What kind of method should I create?<br>
  Maybe another control has a many non-Serializable properties!<br>
  How to duplicate all of them?</p>
  
  <p>Please anyone.....</p>