Did you know that Forms and UserControls can derive from a generic type in .NET 2.0?

For example, you might have a base form like this:

public partial class MyGenericForm<T> : Form
{
/* ... */
}

So this form can be constructed with a type at runtime and its behaviour might differ slightly depending on that type.

Why is that handy? Well, the handiness comes in deriving:

public partial class MyIntForm : MyGenericForm<int>
{
/* ... */
}

Now we have a form that specializes with "int" but inherits all the visual design (and other goodies) from MyGenericForm.

Great in theory, but there's one major catch - doing this means you can't switch to the visual form designer for MyIntForm. Visual Studio just isn't smart enough to construct a visual representation of a form that derives from a generic type.

So are we screwed? Not quite yet!

It's actually possible to fool Visual Studio into thinking that this is a standard, everyday form. You do this by defining a type to sit in between MyGenericForm and MyIntForm:

public class MyCustomIntForm : MyGenericForm<int> { }

So this class does bugger all except derive from MyGenericForm and specialize with the "int" type. Then we adjust our original derived form thusly:

public partial class MyIntForm : MyCustomIntForm
{
/* ... */
}

... and voila! Now we have a form that derives (indirectly) from a generic type, and still works in the visual designer!

This example (using int as the specialized type) is a bit contrived, but I've already found one or two uses for this using my own classes as the type the form is based on. Let me know if it helps you!