Converting Visual Studio 2003 WinForms to Visual Studio 2005/2008/2010/2012 partial classes (via: Duncan Smart’s Weblog)
Posted by jpluimers on 2013/01/10
In the .NET 1.x past, the WinForms designers in Visual Studio .NET and Visual Studio 2003 would put the C# or VB.NET code containing the form code as the InitializeComponent method of the top most class monolithic C# and VB.NET files that also contain the user code (for events and such).
As of Visual Studio 2005 (actually: Whidbey and higher), this code is based on partial classes. For each form (actually designable entity, but lets limit this to WinForms forms) you get a MyForm.cs and MyForm.Designer.cs
As a side note, with a bit of effort, you can generate the Windows Form Designer generated code yourself as this answer shows. This is for instance convenient when you have form definitions in a different technology and want to convert it to WinForms, WPF or another form of designer based .NET code.
I long time ago I wrote a short manual for co-workers on converting the monolithic files (for people interested, it is below).
Since then I found a couple of very interesting links:
- A stackoverflow answer by Martín Marconcini on Split old .NET code into designer partial class which describes pretty much what is below (my way is copy the content, then remove the things you don’t need; his way is to copy over only the stuff that is needed), and links to this:
- A stackoverflow answer by ja72 on c# – How to automatically convert VS2003 classes to partial Designer.cs files? which points to the above question, and points to:
- Converting VB.NET 2003 WinForms to 2005/2008 Partial Classes « Nathan Jones.
a macro based conversion based on: - Converting Visual Studio 2003 WinForms to Visual Studio 2005/2008 partial classes « Duncan Smart’s Weblog.
a macro based conversion - A stackoverflow answer by Fredou on c# – Convert Visual Studio 2003 forms to Visual Studio 2005/2008 forms using partial classes and Designer files that points to the above macro based conversion.
There are two major reasons to move the Windows Forms Designer generated code to Designer.cs files:
- Speed up the iniitialization of the Windows Forms Designer (sometimes saves far more than half the time)
- Lessen the clutter (I’ve seen too many projects where, by accident or inexperience, code got manually added to the InitializeComponent and get lost by the next Designer/Code cycle)
So the above macros are on my research list for the next roundup.
In the mean time, below are the steps I used until now.
While you are still there, a few more links on how Visual Studio copes with multiple files per designable object:
- Visual C#, Winforms, and Partial Class Madness – Stack Overflow.
- visual studio – WinForm partial classes – Stack Overflow.
- c# – Partial nested Form class with designer – Stack Overflow.
Steps
Steps to split off the Windows Forms Designer code from a .NET 1.x C# file into a *.Designer.cs partial class in Visual Studio 2005/2008/2010/2012:
- Start with a .cs/.resx combination of the current form (lets assume they are in SomeFormFile.cs/SomeFormFile.resx)
- (optional, to get naming convention right):
a. Rename the file and the form class to something like MyForm.cs/MyForm.resx and MyForm (where My is a readable PascalCase name for the form)
b. Propagate this name change throughout your projects so they build. - Create an empty MyForm.Designer.cs in the same directory as your MyForm.cs
- In Visual Studio, go to the Solution Explorer, then
a. select MyForm.cs
b. click on the toobar button of the Solution Explorer named “Show All Files”
c. right-click on MyForm.Designer.cs
d. choose “Include in Project” - Copy the content of MyForm.cs to MyForm.Designer.cs
- In MyForm.cs, change the declaration of the form class from:
public class MyForm : System.Windows.Forms.Form
into
public partial class MyForm : System.Windows.Forms.Form
(i.e. replacepublic class
withpublic partial class
) - In the MyForm.Designer.cs, change the form class from:
public class MyForm : System.Windows.Forms.Form
into
partial class MyForm
(i.e. replacepublic class
withpartial class
, then omit the: System.Windows.Forms.Form
and any interface implementations)
Note: If you keep eitherpublic
or the ancestor, the Windows Forms Designer ceases to function. - In the
MyForm.Designer.cs
, remove all using references. - In the
MyForm.Designer.cs
, keep the methods, but remove all the members other than:
a.private System.ComponentModel.IContainer components;
b. private variables (usually they were above the constructor that you removed):
– starting withSystem.Windows.Forms.
– descending fromSystem.Windows.Forms.Control
or fromSystem.ComponentModel.Container
- In the
MyForm.cs
file, remove all members that you kept inMyForm.Designer.cs
- In the
MyForm.Designer.cs
:
a. remove all methods exceptInitializeCompnent()
andDispose()
b. be sure to keep the comments above these two methods, and
c. keep the#region
around theInitializeComponent()
called#region Windows Form Designer generated code
- In the
MyForm.cs
:
a. remove theInitializeCompnent()
andDispose()
methods,
b. remove the comments above these two methods,
c. remove the#region
around theInitializeCompnent()
method. - Save and close the
MyForm.Designer.cs
file, theMyForm.cs
file, and close theMyForm
designer. - Build the solutions
- Open the
MyForm
designer to see if it still works.
–jeroen
IL said
Great! I was digging some old VS2003 project awhile ago in VS2005 and didn’t know I can do this. Could you please elaborate what to remove on step 8?
Also in InitializeCompnent() letter “o” is missing. Thank you!