C# 在 Visual Studio 中的项目之间复制 winform

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/863582/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:42:27  来源:igfitidea点击:

Copying winforms between projects in Visual Studio

c#visual-studiowinforms

提问by Vidar

What is the best way to copy or cut/past a form from one Project to another Project within a Solution in Visual Studio?

在 Visual Studio 的解决方案中,将表单从一个项目复制或剪切/粘贴到另一个项目的最佳方法是什么?

Whenever I try it, using the solution explorer (drag and drop or right clicking cut and paste), it only copies the underlying C# code and not the necessary 'bits and pieces' that help you visualise the form via the form designer.

每当我尝试使用解决方案资源管理器(拖放或右键单击剪切和粘贴)时,它只会复制底层的 C# 代码,而不是帮助您通过表单设计器可视化表单的必要“点点滴滴”。

采纳答案by Vidar

Figured it out - knew it would be something daft.

想通了 - 知道这将是一件愚蠢的事情。

Apparently the target project should have the references:

显然目标项目应该有参考:

  • System.Windows.Forms
  • System.Drawing
  • 系统.Windows.Forms
  • 系统图

included within the project FIRST before you do any copy or pasting or else you will get the problem I described.

在您进行任何复制或粘贴之前首先包含在项目中,否则您将遇到我描述的问题。

Thanks for anyone who tried to help BTW.

感谢任何试图帮助 BTW 的人。

回答by Adam Robinson

Are you selecting the file, or using the designer to "Copy" all of the controls? Selecting the YourForm.csfile in the Solution Explorer and copying it via copy-and-paste or drag-and-drop to your other project should accomplish what you need.

您是选择文件,还是使用设计器“复制”所有控件?YourForm.cs在解决方案资源管理器中选择文件并通过复制粘贴或拖放将其复制到您的其他项目应该可以完成您的需要。

回答by Fredrik M?rk

Make sure that you copy not only the Form.cs, but also the Form.designer.cs and Form.resx files.

确保您不仅复制 Form.cs,还复制 Form.designer.cs 和 Form.resx 文件。

But I need to ask why you would want to maintain a copy of the same form in multiple projects within the same solution. Sounds as if you might want to generalize the code in it, put it in a class library and re-use it in the places where it is applicable.

但是我需要问一下,为什么您要在同一解决方案的多个项目中维护同一表单的副本。听起来好像您可能想要概括其中的代码,将其放入类库并在适用的地方重新使用它。

回答by Chris Doggett

Use ReSharper(get the demo), right-click the class in Code View, Refactor->Move, and move it to another project.

使用ReSharper(获取演示),在代码视图中右键单击该类,Refactor->Move,将其移动到另一个项目。

The added benefit is that if you change the namespace by moving it between projects, it'll auto-update all references.

额外的好处是,如果您通过在项目之间移动来更改命名空间,它将自动更新所有引用。

回答by MORGAN OLSON

  1. Copy the three files, .cs, .designer, resxto the target solution folder.
  2. In the target project, select Add existing itemand add the designer file first.
  3. Modify the Namespace attribute. The .csfile should come in as well.
  4. Modify the namespace in the .csfile.
  5. Add the resxfile using Add existing item.
  1. 复制三个文件,.cs.designerresx到目标解决方案文件夹。
  2. 在目标项目中,首先选择Add existing item并添加设计器文件。
  3. 修改命名空间属性。该.cs文件也应该进来。
  4. 修改.cs文件中的命名空间。
  5. 添加resx使用的文件Add existing item

回答by Tom Wash

It's way easier now in 2012. Just go to File > Add > Existing Project > Drag and drop the form into the project that you want to add it to.

2012 年现在更容易了。只需转到文件 > 添加 > 现有项目 > 将表单拖放到要添加到的项目中。

回答by Nexus23

** This one works for me:

** 这个对我有用:

1) Copy the source files (.cs or vb, .designer, .resx) into target folder

1) 将源文件(.cs 或 vb、.designer、.resx)复制到目标文件夹中

2) Show hidden files in target solution

2)在目标解决方案中显示隐藏文件

3) Select these files and include them into project.

3) 选择这些文件并将它们包含到项目中。

This will add winform or any other partially divided files cleanly.

这将干净地添加 winform 或任何其他部分分割的文件。

回答by Alan

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.

使用 VS2013 刚刚对此进行了测试,它看起来可靠且一致。这类似于上面的一些评论,但添加了另一种更快的方法。

(1st)In Windows File Explorer highlight and copy all 3 Form files (.vb or .cs, .designer, .resx)

(1st)在 Windows 文件资源管理器中突出显示并复制所有 3 个表单文件(.vb 或 .cs、.designer、.resx)

(2nd)This can be accomplished 2 ways:

(2)这可以通过两种方式完成:

(2a-1) In File Explorer paste the 3 files into the project folder with your other forms

(2a-1) 在文件资源管理器中,将 3 个文件与其他表单一起粘贴到项目文件夹中

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

(2a-2) 在 VS 解决方案资源管理器中,打开“显示所有文件”,右键单击粘贴的表单并“包含在项目中”。它应该可以在没有其他更改的情况下工作。

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.

(2b-1) 在 VS 中,单击进入解决方案资源管理器并粘贴 w/Control-C。(出于某种原因,解决方案资源管理器中的右键单击上下文菜单可能不会显示粘贴选项,但它可以通过键盘工作。)此方法将表单直接添加到项目中,而无需如上所述“包含在项目中”。使用此方法,您可以在一个步骤中一次添加任意数量的表单(每个表单都添加 3 个文件)。

回答by Chris Patterson

To copy a form from one VS2013 project to another, the easiest, (fewest mouse clicks) is:

要将表单从一个 VS2013 项目复制到另一个项目,最简单的(最少点击鼠标)是:

  1. In your target project, right mouse click on your parent folder (likely your project) and in the menu select, "Add; Existing Item".

  2. Then select the form's cs file in your source project. For example select Form.cs (not the Form.Designer.cs or Form.resx).

  3. Open Form.cs in your target project and change the namespace (all instances).

  4. Close and reopen Form.cs and you will see all the controls, etc.

  1. 在您的目标项目中,右键单击您的父文件夹(可能是您的项目)并在菜单中选择“添加;现有项目”。

  2. 然后在源项目中选择表单的 cs 文件。例如选择 Form.cs(不是 Form.Designer.cs 或 Form.resx)。

  3. 在目标项目中打开 Form.cs 并更改命名空间(所有实例)。

  4. 关闭并重新打开 Form.cs,您将看到所有控件等。

回答by Ram

I tried the following process in Visual Studio 2012 and it worked.

我在 Visual Studio 2012 中尝试了以下过程并且它起作用了。

  1. Add the project from which you want to import form to your solution, by right clicking on solution > Add > Existing project menus. Select the project and click OK.
  2. Now right click on the form you want to copy, select copy.
  3. Right click on the project where you want to copy and select paste.
  4. The form will be copied to your project, now rename the namespaces.
  5. Remove the project you added in the step 1 from your solution.
  1. 通过右键单击解决方案 > 添加 > 现有项目菜单,将要从中导入表单的项目添加到解决方案中。选择项目并单击确定。
  2. 现在右键单击要复制的表单,选择复制。
  3. 右键单击要复制的项目并选择粘贴。
  4. 表单将被复制到您的项目中,现在重命名命名空间。
  5. 从解决方案中删除您在步骤 1 中添加的项目。