The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My work

  • My badges

  • Twitter Updates

  • My Flickr Stream

    20140508-Delphi-2007--Project-Options--Cannot-Edit-Application-Title-HelpFile-Icon-Theming

    20140430-Fiddler-Filter-Actions-Button-Run-Filterset-now

    20140424-Windows-7-free-disk-space

    More Photos
  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,340 other followers

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:

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:

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:

  1. Start with a .cs/.resx combination of the current form (lets assume they are in SomeFormFile.cs/SomeFormFile.resx)
  2. (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.
  3. Create an empty MyForm.Designer.cs in the same directory as your MyForm.cs
  4. 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”
  5. Copy the content of MyForm.cs to MyForm.Designer.cs
  6. 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. replace public class with public partial class)
  7. In the MyForm.Designer.cs, change the form class from:
    public class MyForm : System.Windows.Forms.Form
    into
    partial class MyForm
    (i.e. replace public class with partial class, then omit the : System.Windows.Forms.Form and any interface implementations)
    Note: If you keep either public or the ancestor, the Windows Forms Designer ceases to function.
  8. In the MyForm.Designer.cs, remove all using references.
  9. 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 with System.Windows.Forms.
    – descending from System.Windows.Forms.Control or from System.ComponentModel.Container
  10. In the MyForm.cs file, remove all members that you kept in MyForm.Designer.cs
  11. In the MyForm.Designer.cs:
    a. remove all methods except InitializeCompnent() and Dispose()
    b. be sure to keep the comments above these two methods, and
    c. keep the #region around the InitializeComponent() called #region Windows Form Designer generated code
  12. In the MyForm.cs:
    a. remove the  InitializeCompnent() and Dispose() methods,
    b. remove the comments above these two methods,
    c. remove the #region around the InitializeCompnent() method.
  13. Save and close the MyForm.Designer.cs file, the MyForm.cs file, and close the MyForm designer.
  14. Build the solutions
  15. Open the MyForm designer to see if it still works.

–jeroen

via: Converting Visual Studio 2003 WinForms to Visual Studio 2005/2008 partial classes « Duncan Smart’s Weblog.

One Response to “Converting Visual Studio 2003 WinForms to Visual Studio 2005/2008/2010/2012 partial classes (via: Duncan Smart’s Weblog)”

  1. 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!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 1,340 other followers

%d bloggers like this: