Importing Windows Forms (C#), Visual Studio 2012
Asked Answered
E

3

16

trying to add an existing Windows form into a VS2012 WinForm project, I go "Add/Existing item..." then import: MyForm.cs, MyForm.designer.cs, MyForm.resx

This used to work in earlier versions of VS. (I may have to adjust the namespaces.)

In VS2012 MyForm.designer.cs and MyForm.resx are now not associated with the Form for purposes of the Designer; i.e. they show up as separate file entries in the Solution Explorer, not nested under MyForm. Double-clicking MyForm shows an empty form (no controls) with no error messages. The project compiles and runs OK.

However, when I drag and drop the same files into the project (adjust/rename the namespace), the form files are properly associated with one another and the Designer opens the form fine.

While I now have a solution, can someone highlight what difference is between these approaches, what is the point of having an Add/Existing Item option if it doesn't work, and what is actually going on when dragging the files? Which file (.csproj perhaps?) is responsible for joining the form files? Because it works both ways in earlier versions of VS can I assume this is a bug?

Thanks.

Ecphonesis answered 25/2, 2013 at 8:23 Comment(0)
P
27

Tried to reproduce your issue - without success.

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.

My steps:

  1. Create WinForms project (in my case .Net Framework is 4) in VS2012 (11.0.05727.1 if matters)
  2. Right-click on projects -> Add -> Existing Item...
  3. Search for any WinForm with controls (I added two forms created in VS2010 for .NET framework 4 and 3.5)
  4. With some delay (approx 30-40 seconds), for both cases "Designer" worked as expected and loaded all existed controls/codebehind
Penates answered 25/2, 2013 at 9:23 Comment(5)
Hmm, please clarify "You should only add Form1.cs..."?? Form1 is automatically generated when a new Windows Form Application is created. I leave it alone... When you add your existing forms, which files do you search and select? All 3 (i.e. .cs, .designer.cs and .resx?) That's what I do anyway. I waited and waited, whether it's 0.4 seconds or 40 seconds it doesn't make a difference, I end up seeing 3 file entries in the Solution Explorer, and the Designer is not working.Ecphonesis
Form1.cs is just a generic name of a Form underlining that you should add only .cs file - not .resx or .designer.cs. Will edit my answer to make it clearPenates
Ok, got ya. If I just select the .cs then it works as you say. Definitely a different behavior than older versions. Maybe in previous versions it also worked with just importing the cs, but it has "forgiven" if one selected the other ones as well. Curious though, it needs the .designer.cs, this is where all the important control stuff lives. Also it includes .resx in the file mask when looking for files to import (but plays up if you actually do :-) I consider it answered. Thanks Nogard.Ecphonesis
While this works perfectly - and I've upticked it, or whatever the verb is - I don't understand how! The CS file doesn't contain any of the code adding controls to the form. Presumably you need the designer file in the same folder? Weird.Screen
Note: This only work with one Form at a time. Selecting multiple Forms .cs files cause it to ignore the designer.cs and .resx files.Alpinist
R
4

Just include the cs file and just wait for about mintue. No other files required to be added. This will work.

go to project > add existing item select the form myform.cs file and then click on the add button. This will import the file in your project.

Rhythmandblues answered 15/1, 2015 at 13:40 Comment(0)
D
0

To import a Form only add Form.cs to the project, do not Form.designer.cs and Form.resx, they are created for you and will cause your form to not work.

Right-click on the Project in Solution Explored, then click on Add, then click Existing Item. Find the form Form.cs to add, add one Form.cs at a time.

Some delay is common for the forms to load, and change the namespaces.

Doc answered 1/9, 2022 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.