Importing Windows Forms (C#), Visual Studio 2012

Matthias picture Matthias · Feb 25, 2013 · Viewed 19.1k times · Source

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.

Answer

Nogard picture Nogard · Feb 25, 2013

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