Helo,
I would like a stuff like that :
public partial class ObjectSelectorControl<T> : UserControl where T : class
The problem is that the designer can't resolve this. Someone as a workaround with this ?
Helo,
I would like a stuff like that :
public partial class ObjectSelectorControl<T> : UserControl where T : class
The problem is that the designer can't resolve this. Someone as a workaround with this ?
There are some restrictions on what your control can or cannot do in order to be able to use the designer. Fundamentally they all revolve around the designer being able to instantiate your class (must have a parameterless constructor, can't be abstract
, etc.). Because the designer has no idea what type to pass as a generic argument (and I doubt this is even a consideration), your class can't be instantiated.
Your best hope would be to create your UserControl
and change the constructor to protected
(this, I believe, will work, since the designer uses reflection and ignores visibility, but I'm not 100% positive). You can then inherit from that UserControl
and create your generic class and call the base (protected
) constructor.
I don't believe this is possible, because the designer invokes an instance of your class. If you use generics, the designer doesn't know what type to pass into 'T'.
Sounds much like what we do in our project.
There's a base class that is generic:
public partial class controlItemList<TBaseItem, TBaseItemCollection> : UserControl, IUIDispatcher
where TBaseItem : new()
where TBaseItemCollection : IItemCollection<TBaseItem>
Then for each use we define a non-generic version (which still couldn't be used by designer):
public class controlMessagesNonGenericParent : controlItemList<MailItem, MailItemCollection>
{
}
... and then we have derived controls that could be used in designer:
public partial class controlMessages : controlMessagesNonGenericParent
{
...
}
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=105876
The bug has been posted to microsoft's site and you can see that its marked as "Postponed" currently there is no solution !! .
Use composition instead of generics. Instead of using ObjectSelectorControl, give a generic member of another type (Selector<T>
maybe) and act on that object instead of trying to force yourself to be generic.
This works
public class Control1<T> : UserControl { ... } public class Control2 : Control1<double> { ... } public class Control3 : Control2 { ... }
had read it here:
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/0c265543-d6f0-41f6-beeb-b89e0071c5c3
Same problem, i resolved with a wrap class
public partial class MyUserControl<T> : UserControl where T : class
public class MyUserControlWrapper : MyUserControl<MyClass>
public class MyUserControlWorking : MyUserControlWrapper
On MyUserControlWorking IDE working fine (on MyUserControlWrapper as error).
Hope help u.