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 12:12

Figured it out - knew it would be something daft.

Apparently the target project should have the references:

  • System.Windows.Forms
  • System.Drawing

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.

查看更多
We Are One
3楼-- · 2019-01-23 12:12

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.

查看更多
家丑人穷心不美
4楼-- · 2019-01-23 12:16

I am using Visual Studio 2010, below are the steps I performed:

  1. Copy all 3 files (.cs, .resx, .Designer.cs) into destination project folder.
  2. At Visual Studio, right click Project -> Add -> Existing Item...
  3. Select all 3 files (.cs, .resx, .Designer.cs), click Add.
  4. Change the namespace on 2 files (.cs, .Designer.cs), if it is different namespace on destination project.
  5. Run the project.
  6. Done!
查看更多
别忘想泡老子
5楼-- · 2019-01-23 12:19

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

  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.

查看更多
beautiful°
6楼-- · 2019-01-23 12:19

If you want to forms in same machine then simply you can do this.

In Solution Explorer Project --> Add --> Add Existing Item.

Select only .cs (Sample.cs) file from source directory and Refresh the current solution Explorer it will Work.

查看更多
等我变得足够好
7楼-- · 2019-01-23 12:22

** This one works for me:

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

2) Show hidden files in target solution

3) Select these files and include them into project.

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

查看更多
登录 后发表回答