C# – Is it Possible to Make a Generic Control in .Net 3.5

cgenericsnetuser-controlswinforms

I've got the following Generic usercontrol declared:

public partial class MessageBase<T> : UserControl
    {
        protected T myEntry;
        public MessageBase()
        {
            InitializeComponent();
        }
        public MessageBase(T newEntry)
        {
            InitializeComponent();
            myEntry = newEntry;
        }    
    }
}

But the compiler won't allow me to do this:

public partial class MessageControl : MessageBase<Post>
{
    public MessageControl()
    {
        InitializeComponent();
    }
}

How do I create a generic user control in C#?

Best Answer

Try this

public partial class MessageControl : MessageControlBase
{    
    public MessageControl()    
    {
        InitializeComponent();    
    }
}

public class MessageControlBase : MessageBase<Post>
{}

The key to getting the designer to work is that the base class of the class you are editing must not be generic.