In Whidbey, the WinForms designer takes advantage of a new C# language feature called “partial classes”. This allows them to pull out the designer generated code into a separate file. It has several advantages:
· Users are less likely to muck with it, since it’s in a different file. When users start to edit designer generated code, things break down pretty quickly. It’s pretty common for the designer to eat code it doesn’t understand, causing you to lose your work.
· The code generator can use no ‘using’ directives, instead having only fully qualified names (FQN). It’s a good idea for code generators to use FQNs, because it makes them resilient to unforeseen changes in the compilation environment (like someone adding a class named “System”
· It reduces clutter in the user file, letting you focus on your work.
To show what this looks like, I created a simple C# Windows Application, and added an OK button. Here is the result:
------- program.cs -------
#region Using directives
static class Program
/// The main entry point for the application.
static void Main()
------- Form1.cs -------
partial class Form1 : Form
------- Form1.Designer.cs -------
partial class Form1
/// Required designer variable.
private System.ComponentModel.IContainer components = null;
/// Clean up any resources being used.
protected override void Dispose(bool disposing)
if (disposing && (components != null))
#region Windows Form Designer generated code
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
private void InitializeComponent()
this.buttonOK = new System.Windows.Forms.Button();
this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonOK.Location = new System.Drawing.Point(205, 238);
this.buttonOK.Name = "buttonOK";
this.buttonOK.TabIndex = 0;
this.buttonOK.Text = "OK";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
private System.Windows.Forms.Button buttonOK;
What do you think of the new default template? It’s an improvement, no?