views:

104

answers:

2

Why doesn't the designer work if you inherit from an own written genericform?

Suppose I've got the following genericform

public class GenericForm<T> : System.Windows.Forms.Form
{
    public T Test
    {
        get;
        set;
    }
}

When I go to the designer I get errors.

The only workaround I made up is using compiler directives.

#if DESIGN    
    public partial class Form1 : System.Windows.Forms.Form
#else
    public partial class Form1 : GenericForm<string>
#endif

    {
        public Form1()
        {
            InitializeComponent();
        }
    }
+2  A: 

The designer has all kinds of "gotchas" like this, unfortunately. There may be a good summary of them somewhere, but I've never got sufficiently embroiled in it to make it necessary.

However, I have run into this particular issue, except applied to a user control. The solution I came up with (which is again pretty hacky, but in a different way) is to derive a concrete class from the generic one. In your case, you'd do:

public class StringForm : GenericForm<string>

You should then be able to use StringForm in the designer without any problems.

Jon Skeet
Makes sense. You also get the same problem with abstract classes :(.
Jonathan C Dickinson
Downvoters: please add an explanatory comment.
Jon Skeet
+1  A: 

I believe that's because the designer tries to instantiate the form (or UserControl) to host it in the designer. If you have a generic (or abstract) form, the designer cannot instantiate it.

Jeremy Wiebe