July 16, 2004

On the way to partial classes, I nearly pulled out all of my hair...

I have an project written in Visual Studio .NET 2003 at home. Around midnight, I get the bright idea to take my two forms and convert them to the newer style of form, which uses partial classes to separate the designer-inserted code and the user-code for a form class.

Great.

The first step was to remove Main() from my form. When you create a new project in Visual Studio .NET 2005, you get a Program.cs file which contains the global main. It looks like this:

#region Using directives

using System;
using System.Collections.Generic;
using System.Windows.Forms;

#endregion

namespace WindowsApplication1
{
     static class Program
     {
          /// <summary>
          /// The main entry point for the application.
          /// </summary>
          [STAThread]
          static void Main()
          {
               Application.EnableVisualStyles();
               Application.EnableRTLMirroring();
               Application.Run(new Form1());
           }
      }
}

The ApplicationModel class above is my experiment, not default code. The second step is to divide your form class into two sections. So, I generated a new project and used it as a reference. I noticed a few things:

  • The designer class dosn't show up in the IDE's file list. I saw in the csproj file (a simple XML file) that the designer file is simply a dependent file to the main form file that you own.
  • The designer file has the definition for Dispose(),
  • It is home to all of your control member variables,
  • It doesn't specify a base class (this is done by your main form class, though no error is generated if you re-specify it... this has some interesting implications).
  • Finally, the designer file declares the components IContainer variable.

So, in about 5 minutes I cut and paste myself into a corner and was left with a form with no controls on it and multiple compilation errors.

I resolved the compilation errors and guess what? I still have a form with no controls on it.

I decide to re-do my entire form, using my old form as a reference. Note that it's about 12:30am and I'm getting tired.

Now I have a form with controls on it, but the designer code is inserted into my regular class file and not the designer file.

Bitch!

I decide to go to bed, leaving my code in ruin. This is almost as bad as going to bed mad at your spouse. I really wanted to make up with the code, but it left me on the couch and a fitful sleep it was.

 

Posted by Nick Codignotto at July 16, 2004 08:24 AM
Posted to Programming
Comments
Post a comment









Remember personal info?






Valid XHTML 1.0!   Valid CSS!