How to easily duplicate a Windows Form in Visual Studio?

WinformsVisual Studio

Winforms Problem Overview


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?

Winforms Solutions


Solution 1 - Winforms

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.

Solution 2 - Winforms

  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.

Solution 3 - Winforms

Secured way without problems is to make Template of your form You can use it in the same project or any other project. and you can add it very easily, such as adding a new form . Here's a way how make Template

1- from File Menu click Export Template

2- Choose Template Type (Choose item template ) and click next

3-Check Form that you want to make a template of it, and click next Twice

4-Rename your template and (put describe , choose icon image ,preview image if you want)

5-click finish

Now you can add new item and choose your template in any project

Solution 4 - Winforms

I have been using another way of copying forms since vb6.

  1. File Menu / SE - Save CurrentForm.cs as - NewForm.cs
  2. Change its Name to NewForm in Properties window.
  3. In Solution Explorer - Add Existing Item - CurrentForm.cs
  4. Usually in MDI form (where CurrentForm is referred) - CurrentFormToolStripMenuItem_Click event - change reference back to CurrentForm (which is automatically changed to NewForm in step 1).

comments welcome.

Solution 5 - Winforms

  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.

Solution 6 - Winforms

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 ;-)

Solution 7 - Winforms

  1. Right-Click on Form -> Copy Class

enter image description here

  1. Right click on destination Folder and Paste Class

enter image description here

  1. Rename new Form and say yes to renaming all references to this class.

enter image description here

Solution 8 - Winforms

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.

Solution 9 - Winforms

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.

Solution 10 - Winforms

1.Add a new folder to your project

2.Copy the form into that

3.Change the name in properties as well as change the file name

4.Check each form for their class name (they shouldn't be same)

Solution 11 - Winforms

(replicated this answer from my other post in case someone is looking for this solution here)

Using VS2013 just tested this and it appears reliable and consistent. This is similar to some comments above but adds another method that's quicker.

(1st) In Windows File Explorer highlight and copy all 3 Form files (.vb or .cs, .designer, .resx)

(2nd) This can be accomplished 2 ways:

(2a-1) In File Explorer paste the 3 files into the project folder with your other forms

(2a-2) In VS Solution Explorer, turn 'Show All Files' on, Right Click on the pasted form & 'Include in Project'. It should work without other changes.

Or, I think better:

(2b-1) In VS, click into Solution Explorer and paste w/Control-C. (For some reason the right-click context menu in Solution Explorer may not show a paste option, but it works from the keyboard.) This method adds the form to the project directly without having to 'Include in Project' as above. With this method you can add as many forms at a time as you like (all 3 files for each) in a single step.

Solution 12 - Winforms

Its Really Easy. "In Design mode FORM" (form1.cs[Design]) copy the whole Form "ctrl A" then ctrl C. All objects at once. Then add a new windows form to the project. Change the size of the form to the size that you want then paste ctrl V all of the new objects will be copied to the new form. When they are all still picked double click on any of the objects. NOT THE FORM!!!..... This will create the code on the Form side matching the objects you just pasted. if it doesn't you can double click on each object and it will create the code one at a time. I use a text box area to double click in and it works almost every time. I use this method everyday WORKS GREAT.

Solution 13 - Winforms

  • Save the all project
  • Right click in the Solution Explorer (SE), "Copy"
  • Right click on the project name in the SE (this is the first line), "Paste". It will create a "Copy of .....vb" form
  • Right click on this new form in SE, "View code", and change its class name to the name, that you wanna use for the Form
  • Left click on the new form in SE and rewrite its name for the one, that you used in the class name (and .vb in the end)
  • Build - if it has no error, you win! :)

Solution 14 - Winforms

  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

Solution 15 - Winforms

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.

Solution 16 - Winforms

Inherit the form!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobin RodricksView Question on Stackoverflow
Solution 1 - WinformsJamesMLVView Answer on Stackoverflow
Solution 2 - WinformsMichael A. McCloskeyView Answer on Stackoverflow
Solution 3 - WinformsMichael azzarView Answer on Stackoverflow
Solution 4 - WinformsAshishAimView Answer on Stackoverflow
Solution 5 - WinformsMusiGenesisView Answer on Stackoverflow
Solution 6 - WinformsEric J.View Answer on Stackoverflow
Solution 7 - WinformsA.ClymerView Answer on Stackoverflow
Solution 8 - WinformsPeter KipeView Answer on Stackoverflow
Solution 9 - WinformsNeil NView Answer on Stackoverflow
Solution 10 - WinformsEmPlusPlusView Answer on Stackoverflow
Solution 11 - WinformsAlanView Answer on Stackoverflow
Solution 12 - WinformsScott Bill YoungView Answer on Stackoverflow
Solution 13 - WinformsdzscView Answer on Stackoverflow
Solution 14 - WinformsDamnScandalousView Answer on Stackoverflow
Solution 15 - WinformsSajjad AbdullahView Answer on Stackoverflow
Solution 16 - WinformsJonView Answer on Stackoverflow