visual-studio 如何在 Visual Studio 中轻松复制 Windows 窗体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1268591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to easily duplicate a Windows Form in Visual Studio?
提问by TheFlash
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?
如何在 Visual Studio 中轻松复制 C#/VB 表单?如果我在解决方案资源管理器中复制并粘贴,它会在内部使用相同的类并搞砸。你怎么做呢?
回答by JamesMLV
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.
我通常在 Windows 资源管理器中复制文件,在记事本/写字板中打开它们,然后更改顶部对类名的提及。将这些文件包含在您的项目中,您就可以开始使用了。
回答by Michael A. McCloskey
- Copy and paste the form.
- Rename the pasted form .cs to match the new form class name. This should auto rename other related files.
- Open up .cs file. Change the class name and the name of the constructor(s) and destructor.
- Open up .Designer.cs file and change the class name.
- 复制并粘贴表格。
- 重命名粘贴的表单 .cs 以匹配新的表单类名称。这应该自动重命名其他相关文件。
- 打开 .cs 文件。更改类名以及构造函数和析构函数的名称。
- 打开 .Designer.cs 文件并更改类名。
Extra Credit:
额外学分:
- Consider abstracting common functionality from the form into common form or controls.
- 考虑将表单中的通用功能抽象为通用表单或控件。
回答by Michael azzar
Secured way without problems is to make Templateof 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 FileMenu click Export Template
1- 从文件菜单中单击导出模板
2- Choose Template Type (Choose item template ) and click next
2-选择模板类型(选择项目模板),然后单击下一步
3-Check Form that you want to make a template of it, and click next Twice
3-检查您要为其制作模板的表单,然后单击下一步两次
4-Rename your template and (put describe , choose icon image ,preview image if you want)
4-重命名您的模板和(如果需要,请放置描述,选择图标图像,预览图像)
5-click finish
5 次点击完成
Now you can add new item and choose your template in any project
现在您可以添加新项目并在任何项目中选择您的模板
回答by MusiGenesis
- Add a sub-folder to your project.
- Right-click on the sub-folder, and click Add Existing Item.
- 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.
- 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.
- 将子文件夹添加到您的项目。
- 右键单击子文件夹,然后单击添加现有项目。
- 浏览到要复制的表单,然后选择其 .cs 文件。这将复制子文件夹中的原始表单(部分和 resx 以及全部)。该名称不会与原始名称冲突,因为子文件夹将包含在其命名空间中。
- 右键单击 .cs 文件,单击 Refactor | 重命名并输入新名称。这也将为您重命名部分和 resx。
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.
我通常反对使用记事本或其他方式打开文件的方法,因为我一直认为像这样的常见任务应该有在 Visual Studio 中执行的内置方法。在这种情况下,有。
回答by Eric J.
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 ;-)
我个人只是剪切和粘贴然后修复任何挥之不去的名称错误。自从我抽象出通用功能以来,我还没有感到足够的痛苦来寻找更好的方法;-)
回答by A.Clymer
回答by AshishAim
I have been using another way of copying forms since vb6.
自 vb6 以来,我一直在使用另一种复制表单的方式。
- File Menu / SE - Save CurrentForm.cs as - NewForm.cs
- Change its Name to NewForm in Properties window.
- In Solution Explorer - Add Existing Item - CurrentForm.cs
- 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).
- 文件菜单 / SE - 将 CurrentForm.cs 保存为 - NewForm.cs
- 在“属性”窗口中将其名称更改为 NewForm。
- 在解决方案资源管理器 - 添加现有项目 - CurrentForm.cs
- 通常在 MDI 表单中(其中引用 CurrentForm) - CurrentFormToolStripMenuItem_Click 事件 - 将引用更改回 CurrentForm(在步骤 1 中自动更改为 NewForm)。
comments welcome.
欢迎评论。
回答by Peter Kipe
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
如果您在 VS 2019 中工作,请花几分钟时间创建一个项目模板——这是一个完美的解决方案。 如何:创建项目模板
Not sure if it applies to earlier versions of VS.
不确定它是否适用于早期版本的 VS。
回答by Neil N
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.
或者更好的是,创建一个各种实现继承的父表单。
回答by EmPlusPlus
1.Add a new folder to your project
1.在你的项目中添加一个新文件夹
2.Copy the form into that
2.把表格复制进去
3.Change the name in properties as well as change the file name
3.更改属性中的名称以及更改文件名
4.Check each form for their class name (they shouldn't be same)
4.检查每个表单的类名(它们不应该相同)


