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

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:
Add the 3 existing form files (cs, Designer.cs and resx).
Now, exclude the 3 files you just added from the project.
Open the Add existing item explorer window and go to your project directory.
Select the cs file and Add.
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.
Add a reference to System.Windows.Forms.dll and System.Drawing.
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.

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)

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";

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.

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.
Open the solution with the project that you want to add your three files.
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
Add a dummy form (or usercontrol) with a dummy name: MyTempForm.
Save the solution and close it in Developer Studio
In a windows explorer delete the three MyTempForm files
Rename your three MyForm files in MyTempForm, with the same extensions.
Open your solution again in Developer Studio 2012
See that your MyForm is now fully available as MyTempForm
In the solution explorer rename MyTempForm back to MyForm

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.

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.

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
Add a new blank form to the project (this automatically adds the references above)
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)
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.

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.

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.

All that is needed in visual Studio 2012 is the following:
Copy the three files for the form into the project folder (.cs, .designer.cs and .resx)
Go to the project and add an existing object.
Select the .CS file
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.

You can still use "Add Existing iTem"
"Add Existing iTem", select three files (.cs .resx .Designer.cs)
make sure "namespace Application" in your .cs file is the same as the project
now you can use auto-complete the Form you just added.

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

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.

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.

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

The simplest solution that works for me is,
Close visual studio if its open
Copy all the 3 files (.cs, .designer.cs, .resx) into your project folder where all the other forms reside too.
Now open the project using visual studio.
Build the project
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.
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.

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

Related

Add additional appsettings.Environment.json under the appsettings.json file [duplicate]

I want to move one code file under the group of another, related file, like here:
As you see, SingleObjectViewModel.Commands.cs is hidden in SingleObjectViewModel.cs group. It can be useful in some cases.
I've created SingleDocumentViewModel.Commands.cs but simple drag-n-drop in Visual Studio do not work.
How to achieve it?
I found one way of doing this, but it is not user-friendly and needs to edit project file by hands. I'm not sure it will work in other versions of Visual Studio then mine (MS Visual Studio 2013)
First, unload project (right-click on project, Unload Project).
Then edit csproj file (right-click again, Edit *.csproj)
In editor, replace:
<Compile Include="SingleDocumentViewModel.Commands.cs">
with
<Compile Include="SingleDocumentViewModel.Commands.cs">
<DependentUpon>SingleDocumentViewModel.cs</DependentUpon>
</Compile>
Then save edited file and load project again (right-click on it, Reload project).
Now files are grouped:

Visual Studio - Web Project - add existing area folder

We have a big Web Project with Many areas. One area is not included into the project, but its folder exists on the disk (and I also added it to Source Control).
I cannot figure out how to add that Area into the project. There seems to be no option for this. I am using Visual Studio 2017.
Above the solution tree is a button to show all files in the folder.
When you click it, your folder will appear in the solution.
On this folder click right "Add to solution".
Now your folder is part of the solution and you can hide the unnecessary items.
You may need to manually edit the csproj file. In windows explorer navigate to your directory and right click to edit the csproj (something like notepad++ works really well). Then locate the ItemGroup section that contains all the elements. Add a new one with your Folder\controller.cs. Save the changes. You'll need to reload the project in VS (if you have it open) to see the changes.
<ItemGroup>
<!--Other compile includes-->
<Compile Include="MissingArea\XController.cs"/>
</ItemGroup>

How to add a new XAML View with code behind

I am using VS 2015, creating a Univerasl App. I want to create a new view (XAML). I can right click, Add > XAML > XAML View, and the XAML gets created with the name and location that I want.
But, how can I create a code behind here, e.g. MyNewView.xaml.cs, and "link it up" as a child node in my solution explorer?
As RavingDev said:
Do not use "XAML View", instead use "Blank Page" or "User Control".
On a side note, if you want to manually create a code file and link it with anything else (i.e. Visual Studio automatically links .cs and .xaml on creation), you'll have to edit the project's XML code.
Assume you created a XAML view/page/control named MyView.xaml and a separate C# file named MyView.xaml.cs, and they're unlinked (this can also happen if you add files directly into the Solution Explorer). To link them, you will have to edit your project's internal code. First, save and quit Visual Studio. Second, find your project file (<project name>.csproj). Open it with a text editor, such as Notepad++, VS Code, or Atom (not Visual Studio). Move down the file until you see ItemGroup elements. There are a few of them, but the one that contains Compile elements is the right one. Add the following code somewhere inside that element:
<Compile Include="MyView.xaml.cs">
<DependentUpon>MyView.xaml</DepenedentUpon>
</Compile>
Do this for every file you want to link.
If everything was done correctly, you can save the file and open it back up in Visual Studio. Your files should now be linked in the Solution Explorer.

In Visual Studio 2013 when I add an existing form then controls don't appear on designer

I'm working on a windows form application. I start a new project. I delete the Form1 stuff from the new project. I go and add from existing in the solution explorer and choose Form1.cs, Form1.Designer.cs, and Form1.resx from another project. When I look at the designer Form1 just looks blank, like a new one would with none of the controls appearing. The program still compiles fine with all it's controls intact. Did I miss a step somewhere? I've closed and reopened the project etc. This happens every single time repeatedly so I believe I'm missing something but searching here or the rest of the web hasn't provided me a solution. Maybe it's just assumed to be known by everyone already?
I am just learning c# and Visual Studio so hopefully I didn't miss something stupid. I was following direction for how to do this from a book. Some projects are re-used to teach a new idea so they have you start a new project and add in existing items from the original to then work on the new idea.
In VS 2013 I chose "Add existing Item" and just selected the .cs files, don't add *.Designer.cs and *.resx files. Then waited for few minutes and restarted VS2013. Designer.cs and resx files appeared as associated with Forms but they were excluded. I right clicked them and chose "Include In Project". Then it worked fine.
Make sure that you are loading the Form1 that you think you are loading.
By default, Visual Studio 2013 (on Windows 7) will create a folder in Documents (C:\Users*Your user name*\Documents) called "Projects". Whenever you create a new project from within Visual Studio by going to File->New Project, Visual Studio will (by default) create a new folder in the Projects folder with the project name.
I think when you "delete" Form1, you're not actually deleting it, just removing it from the project. When you go to add Form1, you're just selecting it from the same project folder, when you actually wanted to load it from a different project folder.
Try this: In your current project (the one where you deleted Form1 and then added it), in the Solution Explorer, right click on the project, and select "Open Folder in File Explorer".
This will open the project folder on disk. Now, delete Form1.cs from within Visual Studio, and switch back to the project folder. If Form1.cs is still in that project folder, then you just removed it from the project. The actual files still exist on disk. When adding existing items, Visual Studio will typically default to the selected project folder.
I have a very strong suspicion that the Form1 that you are really looking for resides in a different project folder. Without knowing the book or tutorial you are following, I can't give you any hints as to where the Form1 you want is located.

Unable to get the designer view window back using windows forms with Visual Studio 2010

I am in process of writing a C# Windows Forms application using Visual Studio Express 2010 ENU SP1. Further VS specifics are at the bottom of this post. I recently made some changes using the designer view to the layout and then decided that I did not want the changes so I closed the designer window. The code window was still up when I closed the project and the changes were saved any way. I don't care about the changes because the layout can be restored easily except for the fact that I cannot find any way to reopen the visual designer window. To get the window back I have tried the following:
Shift F7
Right click in the designer code window
Double click the designer file under the solution explorer
Select the designer file under the solution explorer and use the context menu to bring up the file
I have an evaluation copy of the VS 2010 Team and it displays exactly the same behavior when utilizing my project
I have tried a backup copy of the project but it displays the same problem in both the Express and Team versions
I have been all over the MSDN, VS forum, and the internet at large and not found any solutions
When I first started the project I did rename the Form1.cs file from within the solution explorer because I wanted to have a different name than Form1 for the application. When doing this it asked the following:
"You are renaming a file. Would you like to perform a rename in this project of all references to the code element 'projectname'?
I replied yes. So now I don't have a Form1.cs file but a file with my "projectname.cs" and the designer file named "projectname.Design.cs. This is the only change that I made that I can think of that might be relevant. I did try renaming it back to Form1.cs but that also did not resolve the problem.
I have to say that I am new to using Visual Studio. So far I like it quite a bit but I am dead in the water right now and unless I can get this resolved I will lose two weeks worth of work. Any assistance would be greatly appreciated.
Thanks,
-Tom
Additional Details:
Windows 7 Professional SP1 32 bit x86
Microsoft Visual Studio 2010
Version 10.0.40219.1 SP1Rel
Microsoft .NET Framework
Version 4.0.30319 SP1Rel
Installed Version: C# Express
Microsoft Visual C# 2010 01014-169-2560017-70726
Microsoft Visual C# 2010
Microsoft Visual C# 2010 Express - ENU Service Pack 1 (KB983509) KB983509
This service pack is for Microsoft Visual C# 2010 Express - ENU.
If you later install a more recent service pack, this service pack will be uninstalled automatically.
I realise this is quite an old thread now, but I just had something similar happen in one of my projects in VS2010. There was no way to edit the Form in the form designer.
In the end tracked it down to a problem in the .csproj file. There is a item group that specifies what is compiled. Within the item group tag, for my form it had ...
<Compile Include="AreaChart.cs" />
it needed...
<Compile Include="AreaChart.cs">
<SubType>Form</SubType>
</Compile>
The SubType XML tag was missing. A quick change to the project file and I could edit the form again.
Just one more way to screw up the form designer - I had something like this:
namespace MyProgram
{
public struct SomeStruct {
int a,b;
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
Turns out you can't put the SomeStruct definition there even though everything works fine and there were no errors. Once I moved it, I was able to see the designer view again.
Again it is an old thread but web interest indicates that numerous people are still having this problem and it has not been solved.
I am using VS2012 and had the problem when I copied a library control which spawns 3 forms into a separate library for further development. All controls in the forms displayed perfectly when run but the designer displayed a blank form.
The problem was not as above (that was intact) but, thanks to this hint, I added a new form and compared the entries in the .csproj file. For each of the copied forms, an entry akin to the following was showing:
<Compile Include="MyForm.Designer.cs" />
What was needed was:
<Compile Include="MyForm.Designer.cs">
<DependentUpon>MyForm.cs</DependentUpon>
</Compile>
Adding these manually for each form solved the problem.
Make sure you don't have any class declared before your designer partial class. The .cs file needs to have the partial designer class as the first class in the file.
public partial class frmWidget : Form
{
}
public class SomeClass
{
}
Now SomeClass cannot be on top of the partial frmWidget class. Once you re-arrange that then it will automatically fix the designer issue and no need to restart VS or re-create the form. I hope this helps.
Tried all the suggestions mentioned in this question, nothing worked for Visual Studio Community 2017
no Shift-F7 shotcut
no context menu item visible
no double clicking on MainForm.cs
no wrong class order inside MainForm.cs
no wrong entries in mysolutionname.csproj file
But this worked for me
Right click your MainForm.cs file (or 'Form.cs' depending how you named it)
'Exlude from project'. This temporarily removes MainForm.cs, MainForm.Designer.cs and MainForm.resx
Use 'Add » Existing item' and select MainForm.cs. This will re-include all three files.
Of course you should add them back to the same folder as they were before
Double click on MainForm.cs and Design view magically openes
I had the same problem but mine was caused on VS2012 by dragging and dropping some of the forms into different folders.
David's solution above was helpful towards fixing it, but didn't go all the way.
If you have the form in a sub-folder the Compile Include line should read like this:
<Compile Include="SubFolderName\MyForm.Designer.cs">
<DependentUpon>MyForm.cs</DependentUpon>
</Compile>
Please note that the sub-folder name is needed in the first line but not in the second line.
I also had to change the EmbeddedResource line further down the file to associate the resx file:
<EmbeddedResource Include="SubFolderName\MyForm.resx">
<DependentUpon>MyForm.cs</DependentUpon>
</EmbeddedResource>
Again, note that the first line mentions the sub-folder whereas the second line doesn't.
Thanks for the thoughts. I just completely reinstalled both the Express and evaluation copies of the Team versions of VS 2010 and it is now magically working. I have no idea what caused the problem nor what fixed it but I'm now back on track. I probably should have done that before asking on this forum.
I had the same problem with VB. Thanks for the hints. I found the same issues in the .vbproj with my UserControl.
I had this in the .vbproj file:
<Compile Include="AddCommunicationTask.vb" />
And changed it to this:
<Compile Include="AddCommunicationTask.vb">
<SubType>UserControl</SubType>
</Compile>
When I reloaded it I was still having problems and realised that I had written another class in above the form class, this was causing the error. The form class must be first. All I had to do was move my class down and make the form class the top one.
I hope this helps others - good luck!
p.s - I am using vs Community 2015
I had the same issue since I had created the project as a C# Windows Form App (.NET Core) I decided to create it as a C# Windows Form App (.NET Framework) and I can now see and use the Designer. Note the difference between using 'Core' vs 'Framework'
I hope this helps you out.
I realise this is a really old thread now, but I had the same problem and it turns out there is another node that can go astray in the csproj file; the form .resx has an EmbeddedResource node and mine was missing the subnode Designer. This allowed me to see the form code again in the IDE.
I had a similar issue, but not quite the same.
I copied a form from one project to another (using windows explorer) and then added it to the new project by right clicking on the project in VS and choosing > Add > Existing Item.
My mistake was to add all three files for the form (.cs, .designer.cs and .resx). I should have only added the .cs file.
Although this is quite an old thread, I want to share my case and the solution.
I had the same problem but I used Telerik WinForms component for my Form. I couldn't see the designer until I realize that the design-time capabilities of RadControls for WinForms are implemented in the Telerik.WinControls.UI.Design.dll assembly. Then I add the reference and now the designer shows up.
I had the same problem and I got the (Design) window back by double clicking the Form1.cs file in the Solution Explorer (not the Form1.Designer.cs file).
I accidentally deleted my Form1[Design] file. Double clicking the Form1.h file did nothing. The program ran and the Buttons TextBoxes and Labels were all there just no Form1[Design] was visible to edit. I fixed it Under: Header Files select Form1.h and go to Form1.H file properties. Under File Type from the drop down change it to "C++ Form" I closed and restarted the program. You should now be able to double click Form1.h and see your Form1[design].
I had the same issue in VS 2010. I was not able to view the designer form(Connect.CS).
i already had the
<Compile Include="Connect.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Connect.Designer.cs">
<DependentUpon>Connect.cs</DependentUpon>
</Compile>
tags in the ItemGroup Tag, but still i wasn't able to view the Designer.
I was using name space directive 'Using RFCOMAPILib', which already had a Form element in it and it clashed with the System.Windows.Forms.Form.
After replacing 'public partial class Connect : Form' to 'public partial class Connect : System.Windows.Forms.Form' im able to view the Designer now.

Categories