Clone

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to clone a windows form, give it a new name. How can I do this???
 
Hi Norm,

You have two options and either can be shallow (references are copied) or deep
(data is copied):

public class MyForm : Form
{

1. Method

/// <summary>Creates a shallow copy of the instance.</summary>
public MyForm Copy()
{
return (MyForm) MemberwiseClone();
}

(You can implement ICloneable instead of using a custom method like Copy,
however I prefer the Copy method)

2. Constructor (preferred for deep copy, IMO, but can do both)

/// <summary>Constructs a new deep copy.</summary>
public MyForm(MyForm sourceToCopy)
{
if (sourceToCopy == null)
throw new ArgumentNullException("sourceToCopy");

this.Font = (Font) sourceToCopy.Font.Clone();
this.BackColor = sourceToCopy.BackColor;
...
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top