Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it Possible to Make a Generic Control in .Net 3.5?

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#?

like image 223
Roberto Bonini Avatar asked Dec 27 '08 19:12

Roberto Bonini


3 Answers

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.

like image 92
rnwood Avatar answered Nov 08 '22 15:11

rnwood


For one, althought generic controls are possible under .NET, the Visual Studio designers do not support them, so you're on your own if you want to use them. You'll have to instantiate them yourself in your code and perform layout too.

As for the error you mention, it sounds to me like you're looking in the wrong direction. Perhaps you could write the whole error text here?

like image 1
Vilx- Avatar answered Nov 08 '22 16:11

Vilx-


Yes. its possible. See Below link to get an idea.

https://web.archive.org/web/20130603104810/http://www.hackersbasement.com/csharp/post/2009/08/29/Generic-Control-Builder-in-ASPNet.aspx

like image 2
dfddf Avatar answered Nov 08 '22 16:11

dfddf