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?
This question is related to
winforms
visual-studio
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.
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 ;-)
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)
Extra Credit:
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.
(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.
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.
I have been using another way of copying forms since vb6.
comments welcome.
Inherit the form!
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
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.
Source: Stackoverflow.com