How to easily duplicate a Windows Form in Visual Studio?

Viewed 145095

How can I easily duplicate a C#/VB Form in Visual Studio? If I copy & paste in the Solution Explorer, it uses the same class internally and gets messed up. How do you do it?

16 Answers

I usually copy the files in windows explorer, open them up in Notepad/Wordpad and just change the one mention of the class name at the top. Include those files in your project, and you'll be good to go.

  1. Copy and paste the form.
  2. Rename the pasted form .cs to match the new form class name. This should auto rename other related files.
  3. Open up .cs file. Change the class name and the name of the constructor(s) and destructor.
  4. Open up .Designer.cs file and change the class name.

Extra Credit:

  1. Consider abstracting common functionality from the form into common form or controls.
  1. Add a sub-folder to your project.
  2. Right-click on the sub-folder, and click Add Existing Item.
  3. Browse to the form you want to copy, and select its .cs file. This will duplicate the original form (partial and resx and all) in the sub-folder. The name will not conflict with the original, because the sub-folder will be included in its namespace.
  4. Right-click on the .cs file, click Refactor | Rename and enter the new name. This will also rename the partial and the resx for you.

I'm generally averse to methods of doing this that involve opening up the files in notepad or whatever, since I always think a common task like this should have a built-in way of doing it in Visual Studio. In this case, there is.

First of all, if you're duplicating a lot of forms with cut and paste, consider a common base class for your forms (or for a category of your forms) that implements shared/common functionality or look & feel elements. You can also create a template for new forms that meet your needs and create new forms from that template.

Personally I just cut and paste then fix any lingering name errors. Since I abstract out common functionality, I have not felt enough pain to look for a better way ;-)

If you're working in VS 2019, take a few minutes to create an item template -- it's a perfect solution. How to: Create item templates

Not sure if it applies to earlier versions of VS.

  1. Press "Ctrl" and drag your mouse to duplicate a existing form class file.
  2. Then exlude the existing form class file from the project.
  3. Rename the new form class file. Go to code behind, rename the class name and its related reference together.
  4. Include back the previous existing form class file of project
  5. Done

Just rename the class the designer references.

But a better solution is to create a new instance of the same class at run time.

Or better yet, create a parent form that various implementations inherit from.

This process may take one or two minutes...

  • Create a new project.
  • Copy the form's files to the new project's folder.
  • Include the form in the new project.
  • Rename the form.
  • Rename its class name in the code behind file.
  • Rename constructor name in the code behind file.
  • Rename class name in the designer file.
  • Bring the changed form's files back in the folder of original project and include it.
Related