Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying winforms between projects in Visual Studio

What is the best way to copy or cut/paste 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 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.

like image 535
Vidar Avatar asked May 14 '09 14:05

Vidar


People also ask

How do I add a form from one project to another in Visual Studio?

Select Visual Studio menu Project -> Add Existing Item... Then select (browse) in the file dialog the Form. cs (Form = name of the form) you want to add to the project and the form (with all associated files) is (copied and) added to your project.

How do I copy a form from one project to another in VB net?

Right click on your project file in Solution Explorer and choose Add -> Existing Item. Show activity on this post. You need to copy all the files. You should even be able to open two instances of Visual Studio and drag and drop the form from one to the other.


11 Answers

  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.
like image 186
MORGAN OLSON Avatar answered Oct 01 '22 00:10

MORGAN OLSON


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.

like image 31
Chris Patterson Avatar answered Oct 01 '22 00:10

Chris Patterson


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.

like image 20
Vidar Avatar answered Sep 30 '22 22:09

Vidar


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.

like image 36
Tom Wash Avatar answered Sep 30 '22 23:09

Tom Wash


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.
like image 27
Ram Avatar answered Sep 30 '22 23:09

Ram


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

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.

like image 44
Fredrik Mörk Avatar answered Oct 01 '22 00:10

Fredrik Mörk


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.

like image 37
Chris Doggett Avatar answered Sep 30 '22 23:09

Chris Doggett


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.

like image 26
Anto sujesh Avatar answered Sep 30 '22 22:09

Anto sujesh


I finally figured out as followings:

1) copy 3 files of your wished form whose file extension are .cs, .resx, .Designer.cs.
2) Add Existing and select formName.cs.
3) check the namespace and Name property value of the added form.
4) In Program.cs file, use using NameSpace; and replace new NameSpace.formName() in the line of Application.Run().

like image 34
Joywins Avatar answered Sep 30 '22 23:09

Joywins


For Visual Studio V.12 this is the only method which worked for me (It is a mix of above entries):

  • Copy through Files Explorer the three files that the form is made up.
  • At the top bar of the solution explorer in Visual Studio, turn on "show all files" .
  • The file.cs file appears in grey. Click right-button and select: "Include in Project". The solution explorer automatically links the three file (.designer and .resx under .cs file). The file.cs change icon to from c++ to form icon.
like image 35
Juan Jose Sanchez Avatar answered Sep 30 '22 22:09

Juan Jose Sanchez


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!
like image 25
LifeiSHot Avatar answered Sep 30 '22 23:09

LifeiSHot