How do you add an existing form to a new project?

I have never been able to successfully add a Form from an existing file to a new project.

I read on a blog that you add just the *.cs file and the dependencies come in. Well, I tried this and the file did dragin and associate the *.designer and *.resx files. But, the Form icon does not display. Instead, the file looks like a normal *.cs file image. And, when I double click the file I get the code behind instead of the form object.

Is it possible to add existing Forms and get them properly recognized?


Here's an approach that doesn't involve closing the project and reopening it:

  1. Add the 3 existing form files (cs, Designer.cs and resx).
  2. Now, exclude the 3 files you just added from the project.
  3. Open the Add existing item explorer window and go to your project directory.
  4. Select the cs file and Add.
  5. Tada. all good

After some more research I discovered the source of the issue. It is correct that all one has to import is the *.cs file. The caveat lies in the project type. If working in a Class Library project, another step must be performed.

  1. Add a reference to System.Windows.Forms.dll and System.Drawing.
  2. Import the *.cs file

Notes:

A. The files are only properly recognized after I performed these steps and then tried to open the file. Suddenly VS "wakes up" and fixes the files.

B. Order of the steps does not matter. If you already imported *.cs files, just fix the references.

C. If one is missing other references e.g. DevExpress or other 3rd party control imports), the *.cs files will not display properly until this has been resolved.


Sorry P.Brian.Mackey, your solution didn't work for me. It did improve it by getting VS to recognise that it was a form rather than a code file, i.e. gave it is Icon (imagine it added Form)

But only way I managed to fix the issue fully was to edit the csproj file manually in a text editor. I'm not really convinced that this is a great solution and is potentially quite dangerous, especially given I made two typing mistakes and completely broke the project but it did work once I got it right. It’s also hard to find the mistakes like not closing the tags properly or forgetting a file extention.

So the project started out with these entries after adding the files via 'Add --> Existing Item' :(P.s. I'm certain you don't have to copy the files into the project folders first, just navigate to where they are stored outside the project and VS will copy them to the folder which you right clicked on. Copy in advance of course works too.)

<Compile Include="Reports\GBudget1Report.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="Reports\GBudget1Report.Designer.cs" />
<Compile Include="Reports\GBudget2Report.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="Reports\GBudget2Report.Designer.cs" />
<Compile Include="Reports\LASHForm.cs">
  <SubType>Form</SubType>
</Compile>

and further down in the file:

<EmbeddedResource Include="Reports\GBudget1Report.resx" />
<EmbeddedResource Include="Reports\GBudget2Report.resx" />

Comparing these with the existing forms in the project which were working correctly (other option is if you haven't got one, is to create a new form in Visual Studio and it'll give you the correct mark-up to copy) I discovered that the DependentUpon tag isn't associating the sub files to the main form code file.

So I edited it by hand to match:

<Compile Include="Reports\GBudget1Report.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="Reports\GBudget1Report.Designer.cs">
   <DependentUpon>GBudget1Report.cs</DependentUpon>
</Compile>
<Compile Include="Reports\GBudget2Report.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="Reports\GBudget2Report.Designer.cs">
   <DependentUpon>GBudget2Report.cs</DependentUpon>
</Compile>

Again further down the file:

<EmbeddedResource Include="Reports\GBudget1Report.resx">
  <DependentUpon>GBudget1Report.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Reports\GBudget2Report.resx">
  <DependentUpon>GBudget2Report.cs</DependentUpon>
</EmbeddedResource>

It's not a solution I like, but it works. If anyone has any better solutions, I'm happy to listen.


You can import an existing Form into a project. The files that need to be imported depend on the version of the Visual Studio used to create the form.

I will assume that you have two separate projects, Foo and Bar, in different solutions:

C:\
    Projects\
        Foo\
            Foo.sln
            Foo.vcproj
            Program.cs
            Foo.cs
            Foo.Designer.cs
            Foo.Designer.resx

and

C:\
    Projects\
        Bar\
            Bar.sln
            Bar.vcproj
            Program.cs
            Bar.cs
            Bar.Designer.cs
            Bar.Designer.resx

Now, say that you need to import fhe form Foo to the project Bar. First, you need to copy all files that accompany Foo into the Bar project folder:

C:\
    Projects\
        Bar\
            Bar.sln
            Bar.vcproj
            Program.cs
            Bar.cs
            Bar.Designer.cs
            Bar.Designer.resx
            Foo.cs
            Foo.Designer.cs
            Foo.Designer.resx

Then, open Bar.sln in Visual Studio and right-click on the Bar project in Solution Explorer. Select [Add existing item] and select all files that you copied for the Foo form in the dialog opened. After confirming, you should see the newly imported form correctly in Solution Explorer.


I've just encountered similar issues when upgrading VisualBasic forms, going from VisualStudio 2010 to VisualStudio 2013. There appear to be two ways to add existing items.

Problem: If I choose Main Window->Project->Add Existing Item and pull in only the file formname.vb, the result appears to be interpreted as code only (no designer), points to the original file (rather than taking a copy to the new project) and has other issues.

Solution: If instead, I go to the Solution Explorer window, click to select the project (as opposed to the lower level objects in the tree) and then right click in the window, the resulting menu offers Add->Existing Item. Using this version works as expected, requiring only that I locate the formname.vb file. No manually copying files, no pointing to mulitple files, no editing scripts, etc. I'd guess the same applies for forms written in C.