WindowsForms designer and GenericForm

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();
        }
    }

Answers


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.


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.


Need Your Help

How to split a text by a whole line using regex

c# regex

I have a text, something like this:

Passing argument surrounded with double quotes

c# command-line parameters

I've read many solutions to this problem and have tried all of them but cannot find the correct way to accomplish this task. My code is:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.