Copying winforms between projects in Visual Studio

2019-01-23 11:58发布

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

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

14条回答
贪生不怕死
2楼-- · 2019-01-23 11:59
  1. Copy the three files, .cs, .designer, resx to the target solution folder.
  2. In the target project, select Add existing item and add the designer file first.
  3. Modify the Namespace attribute. The .cs file should come in as well.
  4. Modify the namespace in the .cs file.
  5. Add the resx file using Add existing item.
查看更多
Melony?
3楼-- · 2019-01-23 12:00

I have tried the below steps and its worked fine.

  1. copy all the 3 files and icon files (if any) to the target project.
  2. Now go to your project solution explorer and click the show all files icon in the top of the sol.explorer
  3. now you can see your newly added files showing in your project.
查看更多
beautiful°
4楼-- · 2019-01-23 12:02

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

  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.
查看更多
beautiful°
5楼-- · 2019-01-23 12:04

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

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

查看更多
迷人小祖宗
6楼-- · 2019-01-23 12:07

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

查看更多
Ridiculous、
7楼-- · 2019-01-23 12:10

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.

查看更多
登录 后发表回答