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#?
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.
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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With