52

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?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • 1
    Take a look at the actual `csproj` file itself. It is XML and can be hand edited - see how existing forms are structured in it and duplicate the structure for the new form. – Oded Apr 25 '12 at 13:30
  • This has always worked for me, never had any issues, are you perhaps trying to add a form to console project? Seems very strange that it doesn't work. – Jason Apr 25 '12 at 13:32
  • 1
    I can say that in VS2012 the only solution that worked for me was the one to edit the `.csproj` file and create the dependencies manually (not too hard). Otherwise the `.Designer.cs` and `.resx` files never showed up underneath the form properly in Solution Explorer. Solution by @MatthewRadford was the only one that worked for me. – atconway Jul 01 '13 at 22:12
  • 1
    Possible duplicate of [Copying winforms between projects in Visual Studio](http://stackoverflow.com/questions/863582/copying-winforms-between-projects-in-visual-studio) –  Dec 30 '15 at 15:49
  • Add the forms by clicking add existing items. The forms will be added without the form icons. Close the solution and reopen it. The forms will be loaded with default icon. This worked for me in VS 2015. – Rahul Mar 02 '17 at 12:39
  • This solution worked for me: https://stackoverflow.com/a/4905185/6186647, Only add Form.cs file. – Milad Jun 25 '17 at 08:18

22 Answers22

44

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
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
brenth
  • 541
  • 4
  • 3
37

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.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • 2
    I also had to close and re-open my solution after adding the references. And then re-add the .cs files – Koenyn Nov 28 '12 at 06:10
  • Since there is literally two files called `*.cs`, to clarify it is not the `*.designer.cs` file. – Deantwo Nov 26 '20 at 10:52
26

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.

Matthew R
  • 1,038
  • 11
  • 15
  • 2
    This is the only solution I found to work in VS2012 for adding existing forms to a new project after copying over and adding them to the project. – atconway Jul 01 '13 at 22:14
5

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.

npclaudiu
  • 2,401
  • 1
  • 18
  • 19
  • In my case, there are three files too but the naming is a bit different. I'm on VS2010 Ultimate and the file names are: `MyForm1.cs`, `MyForm1.Designer.cs`, `MyForm1.resx`. As far I can see, there's no `MyForm1.Designer.resx` anywhere. Is that a typo? What version of VS are you on? – Konrad Viltersten Oct 16 '12 at 14:56
  • It seems that they dropped the ".Designer" part in .resx file name in VS 2010. Anyway, this should still work. – npclaudiu Oct 16 '12 at 19:19
  • Why, of course it did. And it was +1 for the very clear file tree layout, too. I was just curious what version you were on for **those** file names. Might be nice to know just in case. I didn't mean to criticize - sorry if it sounded like that. – Konrad Viltersten Oct 17 '12 at 04:14
  • The directory layout is for VS 2008. I forgot to mention that, sorry. – npclaudiu Oct 17 '12 at 20:05
3

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.

James
  • 31
  • 2
2

You need actually 2 files: - Form1.cs - Form1.Designer.cs

Copy - paste them to your new project (just make sure there is no such form with the same nameexisting in new project)

Mitja Bonca
  • 4,268
  • 5
  • 24
  • 30
1

Go to solution explorer and right click on it then add existing item, here select the existing form path. Now, and this is important, on the page where you want to use existing form, you must add header file:

using "your existing project name";
Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82
agha
  • 11
  • 1
  • As part of testing, I like to create a user control on its own test solution. Once I'm happy with it, I then copy the user control files (*.cs, *.resx, *.designer.cs) to the target folder. I then use NtePad++ to edit the designer.cs file changing the namespace. For example, let's say my testProject was Tester and my target project is Boo. In the designer.cs file, I change `namespace testProject` to `namespace Boo`. – Jim Lahman Oct 31 '13 at 14:51
1

If you want to create a library of windows forms it is better if you create a Windows Forms Application project. Then delete the default form (Form1), go to the project properties and change the Output Type (or project type) from Windows Forms Application to Class Library.

This way the output will be a DLL but it will have the references you need for a windows forms project. As you pointed out, when adding existing items do NOT add their corresponding .Designer and .resx files, just add their top level/main file.

Lord of Scripts
  • 3,579
  • 5
  • 41
  • 62
1

Maybe it is because of using visual studio 2012, but all these solutions didn't work. AtConway gave a hint to edit the csproj file. And that hint worked.

  1. Open the solution with the project that you want to add your three files.
  2. Let's assume you want to add MyForm.cs / MyForm.Designer.cs and MyForm.resX. Make sure they are in the folder of your project
  3. Add a dummy form (or usercontrol) with a dummy name: MyTempForm.
  4. Save the solution and close it in Developer Studio
  5. In a windows explorer delete the three MyTempForm files
  6. Rename your three MyForm files in MyTempForm, with the same extensions.
  7. Open your solution again in Developer Studio 2012
  8. See that your MyForm is now fully available as MyTempForm
  9. In the solution explorer rename MyTempForm back to MyForm
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
1

Assume that you want to import a Form called YourFormName. Then you should only add YourFormName.cs to the project - the rest parts (YourFormName.designer.cs and YourFormName.resx) will be added automatically.

steps:

Create WinForms project (in my case .Net Framework is 4.5.1) in VS2013 Right-click on projects -> Add -> Existing Item... copy winform you want to import to the folder of your project. If you want to add it to a new folder then first create a folder then add. Search for any WinForm with controls (I added two forms created in VS2010 for .NET framework 4) in lesser than 10 sec visual studio added all the remaining files and i can open this in design mode.

ankur
  • 73
  • 1
  • 7
1

After trying several methods the easiest way for me to use an existing WinForm was found to be: (similar to HaraldDuch’s answer) and tested only on VS2013:

1) Before moving your existing WinForm to the destination project’s source folder, create a dummy WinForm using the same name of your source WinForm.

2) Close your VS solution while saving.

3) Delete/Replace the newly created dummy set of WinForm files (*.cs, *.Designer.cs and *.resx) from Windows Explorer (off VS IDE) with the existing WinForm files you want to use.

4) Open VS to find your existing WinForm and you can rename it from VS IDE if you wish and you will need to change the namespace of the old WinForm to match your new project.

Prasa
  • 51
  • 3
  • This worked for me in VS2015. Had to make a few edits in Designer to change project name. – Tim Apr 20 '17 at 17:55
1

After doing a lot of testing and failing to correctly recognise the form when added, even though .designer and .resx were copying through when .vb was imported, I found Vidar's solution the best, least hacky fix.

It appears that if these references are not already in the project (if there are no forms yet), the form is not recognised as a form and is imported as a code module instead.

  • System.Drawing
  • System.Windows.Forms

These can either be added manually per their solution above, or you can simply

  1. Add a new blank form to the project (this automatically adds the references above)
  2. Add existing form .vb/.cs you want to import (it will now show the form icon in the Solution Explorer and 'View Designer' will be available from context)
  3. Delete the blank form or use it for something else

This does not require editing any of the VS config files as shown in other answers to the question.

Community
  • 1
  • 1
drewdqueue
  • 60
  • 1
  • 5
1

I tried almost all of the proposed solutions but none of them really worked for me. I finally ended up editing the csproj file myself. After I added the forms from another project into my new project, I opened the csproj file and I noticed that there was a missing part to the reference that was added by Visual Studio. Therefore, I edited the node according to the following template.

<Compile Include="NewForm.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="NewForm.Designer.cs">
  <DependentUpon>NewForm.cs</DependentUpon>
</Compile>

Then I restarted Visual Studio and everything worked like a charm.

0

In my case, I have also noted that I can only ad one form at a time. You will get to see the forms as seperate items if you try to add multiple forms at the same time.

brian
  • 1
0

All that is needed in visual Studio 2012 is the following:

  1. Copy the three files for the form into the project folder (.cs, .designer.cs and .resx)
  2. Go to the project and add an existing object.
  3. Select the .CS file
  4. It will import and look like a standard code file. Double-clicking on it will not open the designer as mentioned above. However just click on save all files and then close and re-open the project, now it is recognized as a form and works correctly.
Robb
  • 11
0

You can still use "Add Existing iTem"

  1. "Add Existing iTem", select three files (.cs .resx .Designer.cs)

  2. make sure "namespace Application" in your .cs file is the same as the project

  3. now you can use auto-complete the Form you just added.

0

This worked for me just now (I have the same problem as you). 1. Created new winform with the same name, added something to it (so the resx file is created), saved and closed. 2. Replace newly created files with your original form. 3. Done

Jan Macháček
  • 612
  • 7
  • 11
0

Drag and drop .cs files from filesystem to project tree in Solution Explorer (for example):

  • mainForm.cs
  • mainForm.Designer.cs

Don't forget about references if it is not Windows Forms project.

0

Although this is an old post, I was having the exact same problem as @MatthewRadford had described above. My workaround for this was to only add the .CS file (do not add the .resx file), and allow visual studio to automatically generate the .resx file right after adding the .cs file.

However, I found a permanent solution. If you began to experience this problem immediately after upgrading to a new version of visual studio, it is possible that during the upgrade, Active Reports was not registered properly. The solution is to deactivate your Active Reports (using GrapeCity License Manager), uninstall Active Reports, reinstall Active Reports from scratch, and reactivate your license. You should now be able to add the ReportName.cs and ReportName.resx, as well as the Designer.cs file, all at the same time, while having visual studio correctly handle their dependencies.

Pavel
  • 704
  • 11
  • 25
0

Just add the form file(form.vb or form.cs) other files will be created automatically.

Somasekar
  • 103
  • 1
  • 3
  • 13
0

The simplest solution that works for me is,

  1. Close visual studio if its open

  2. Copy all the 3 files (.cs, .designer.cs, .resx) into your project folder where all the other forms reside too.

  3. Now open the project using visual studio.

  4. Build the project

  5. Open the form by right clicking it and selecting view designer, this will generate the proper designer.cs for the form. If you don't do this sometimes it might give an error saying

InitializeComponent doesnt exist int the current context.

  1. Now you may open and edit the .cs code file

The new form will appear like a normal form in the solution explorer here after.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Subbu
  • 588
  • 1
  • 7
  • 18
-2

All you have to is to pass the .cs file. (The code file)

FTheGodfather
  • 902
  • 7
  • 6