How to easily duplicate a Windows Form in Visual Studio?

前端 未结 14 1423
梦谈多话
梦谈多话 2021-01-30 21:03

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?

相关标签:
14条回答
  • 2021-01-30 21:24

    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.

    0 讨论(0)
  • 2021-01-30 21:27
    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.
    0 讨论(0)
  • 2021-01-30 21:29

    (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 讨论(0)
  • 2021-01-30 21:31

    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

    0 讨论(0)
  • Inherit the form!

    0 讨论(0)
  • 2021-01-30 21:33
    1. Right-Click on Form -> Copy Class

    1. Right click on destination Folder and Paste Class

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

    0 讨论(0)
提交回复
热议问题