49
votes

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
Possible duplicate of Copying winforms between projects in Visual Studiouser565869

16 Answers

54
votes

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.

22
votes
  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.
10
votes

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

5
votes
  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.

4
votes

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.

3
votes

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

2
votes
  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

1
votes

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.

0
votes

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.

0
votes

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)

0
votes

(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.

0
votes

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.

0
votes
  • 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! :)
0
votes
  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
0
votes

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.
-1
votes

Inherit the form!