43

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.

Vidar
  • 6,548
  • 22
  • 66
  • 96
  • What "bits and pieces" are you missing? Is it copying the Designer.cs and .resx files, or just the YourForm.cs file? – Adam Robinson May 14 '09 at 14:26
  • Kind of duplicate - would it be better if I put my solution under "Gold's" question and then deleted this question off SO??? – Vidar May 14 '09 at 14:42

22 Answers22

62
  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.
Marko
  • 20,385
  • 13
  • 48
  • 64
MORGAN OLSON
  • 644
  • 6
  • 2
  • Thank you, worked fine. I was add the Form1.cs file first – Jack Aug 22 '15 at 18:21
  • 29
    In VS2013, it is even easier, just copy the .cs, .designer, and .resx form files to the target solution folder. Use 'Add Existing Item' as above BUT ADD THE .cs file. That's it! All 3 files brought in AND namespace is automatically changed! – Joe Gayetty Jan 13 '16 at 13:41
  • 1
    Agreed - this works and is easy. Wish he would edit his answer - also wish this was top answer/accepted. – B. Shea Dec 10 '16 at 23:05
  • 4
    I will mention though - my namespace still didn't update on .cs or .designer when adding .cs first. But close enough. – B. Shea Dec 10 '16 at 23:14
  • This is good also for when you have two separate, isolated solutions that you're opening in two instances of Visual Studio. – Panzercrisis May 24 '17 at 18:13
  • 2
    @JoeGayetty This still works in Visual Studio 2017 as well. Just to state the obvious: you will of course have to update References, Properties, etc. – BernieP May 01 '18 at 14:29
  • I've put this as the accepted answer - but don't know for sure whether it is as I haven't tested it myself - but I trust the community. – Vidar Mar 24 '21 at 15:48
22

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.

Chris Patterson
  • 581
  • 3
  • 9
11

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.

Vidar
  • 6,548
  • 22
  • 66
  • 96
9

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.
Ram
  • 1,225
  • 2
  • 24
  • 48
8

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.

Tom Wash
  • 81
  • 1
  • 2
4

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.

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • No I just designed a form in the wrong project and now I want to move it into the correct project. – Vidar May 14 '09 at 14:28
  • I also added the designer.cs and resx files too - still doesnt change anything. – Vidar May 14 '09 at 14:29
  • OK; I just wanted to raise the flag since you used the word "copy" instead of "move" :o) – Fredrik Mörk May 14 '09 at 14:30
  • 1
    I just tried it (in VS 2008). If I drag the Form.cs file while holding the key, it will move the form and its associated files to the other project, and behave like a form in that project. – Fredrik Mörk May 14 '09 at 14:32
4

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.

Chris Doggett
  • 19,959
  • 4
  • 61
  • 86
  • It actually has good support for copying forms - currently using ReSharper - and liking it a lot. – Vidar Feb 14 '12 at 10:42
3

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.

Anto sujesh
  • 325
  • 3
  • 14
  • The file open dialog also allows you to add the file as a link. There's a drop down next to the OK/Cancel buttons. – Steve Apr 04 '19 at 15:47
2

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

stevieb
  • 9,065
  • 3
  • 26
  • 36
Joywins
  • 21
  • 4
1

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

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!
LifeiSHot
  • 141
  • 2
  • 4
0

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

Nexus23
  • 6,195
  • 9
  • 50
  • 67
0

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.

Alan
  • 1,587
  • 3
  • 23
  • 43
0

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.
SaddamBinSyed
  • 553
  • 3
  • 17
0

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.

vezunchik
  • 3,669
  • 3
  • 16
  • 25
0

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

ranojan
  • 819
  • 8
  • 11
0

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

0

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.

B H
  • 1,730
  • 18
  • 24
0

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.

cmartin
  • 2,819
  • 1
  • 26
  • 31
0
  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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
navinrangar
  • 904
  • 10
  • 20
0

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.
Holly M
  • 11
  • 5
0

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.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343