Copying winforms between projects in Visual Studio

Viewed 100507

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.

22 Answers

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.

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.

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.

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.

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.

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

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.

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.

If you are using VS 2015, then you just want to add ".vb" file and change the name space with new project name.

Ex: If you want to add "Form1" that created using VS2008 to new project that created using VS2015.

  1. Copy all ".vb" file, ".resx" file, ".disigner.vb" file to the new project folder.
  2. Then change the name space of ".disigner.vb" file using new project name.

These two steps worked for me.

In visual Studio 2015, click on solution explorer and right click on mouse Add->Existing Item and select from another project which form you want to add like Form.cs and automatically Form.designer.cs , Form.resx will be added

If you need use the same form in differents projects, the solutions is: Create a "Class Library Project", to this proyect you add the forms what you need share, then just add the reference to the dll to your main project, you will can make reference to forms easily. Its works perfect for me

The 'easiest' way I have found to do this is to:

  1. Create a form with the same name in your new project
  2. Add a button or some other control to the new form (this creates the .resx file)
  3. Save and close your new project
  4. Copy the MyForm.cs, MyForm.designer.cs, MyForm.resx files from the old project to overwrite your new project
  5. BEFORE opening the new project in VS, edit the namespaces in MyForm.cs and MyForm.designer.cs using a text editor to match your new project namespace
  6. At this point, you can open the new project in VS and everything should look right

I have never had any luck trying to copy the files and 'adding existing item' because the hierarchy doesn't seem to rebuild. Maybe adding the .designer.cs first as another user suggested? Anyway, if you try it that way, you can manually edit the new project .csproj file to include the hierarchy entries from the original project that look something like this:

<Compile Include="MyForm.Designer.cs">
  <DependentUpon>MyForm.cs</DependentUpon>
</Compile>

Worth noting that it's really easy to copy the controls between forms in two different projects. Just select and Ctrl+C in the source project, then select the form in the new project and Ctrl+V.

None of these 'automatic' options worked for me in VS2019. The form designer would always be blank and the pasted files didn't combine correctly in the Solution Explorer. The way I did it in VS2019 was the following 3 steps:

  1. Copy the form files to the new project directory using windows explorer. For example, these 3 files:
  • StartForm.cs
  • StartForm.Designer.cs
  • StartForm.resx
  1. If your namespace is different, open both .cs files in notepad and change the namespaces to the new namespace. (Use ctrl+F to make sure you don't miss any references) I didn't have to do this because my namespace was the same in my new project.

  2. Edit both the csproj files in Notepad and copy the references for these files from one csproj file to the new csproj file. For example, this is what I had to copy:

<ItemGroup>
...
<Compile Include="StartForm.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="StartForm.Designer.cs">
  <DependentUpon>StartForm.cs</DependentUpon>
</Compile>
<EmbeddedResource Include="StartForm.resx">
  <DependentUpon>StartForm.cs</DependentUpon>
</EmbeddedResource>
...
</ItemGroup>

After this, if you have the new project open in VS it will recognize a change and ask if you want to reload.

  1. Go to visual studio and set up your new project. A new file will be created in the selected destination.

  2. Now go to the source file of which data you want to export to the project you just created. Copy all the files from the source file.

  3. Go to the new project folder and paste all the files there.

  4. Now as these files are already present there, option to replace the new project file will be shown. just click on the replace existing files.

  5. Now, check the project you've just created. all the changes would be appeared there.

Credit to Harry Davis' article on https://quick-adviser.com. Had the same issue, but none of the solutions here worked for me. I'll also admit that I didn't fully understand, as I just started using VS a week ago and wanted this for a class project. This solution worked for me for VS 2021.

  1. Add a sub-folder to the project. I did this by right-click on the project name from the Solution Explorer window and Add > New Folder.
  2. Right-click on the new sub-folder and click Add > Existing Item.
  3. Browse for the .cs file you want to copy (not the Design.cs, not anything else) and click Add.
  4. Right-click on the newly added .cs file and click Rename.
Related