How to easily duplicate a Windows Form in Visual Studio?

前端 未结 14 1422
梦谈多话
梦谈多话 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:12

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

    0 讨论(0)
  • 2021-01-30 21:16

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

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

    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.

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