Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic base class for WinForm UserControl

I created a generic base class for a WinForm UserControl:

public partial class BaseUserControl<T> : UserControl {     public virtual void MyMethod<T>()      {          // some base stuff here      } } 

And a UserControl based on that:

public partial class MyControl : BaseUserControl<SomeClass> {     public override void MyMethod<SomeClass>()      {          // some specific stuff here          base.MyMethod<SomeClass>();     } } 

It works fine, but MyControl cannot be edited in the VisualStudio Designer, because it says it cannot load the base class. I tried to define another class BaseUserControl, non generic, hoping it would load it, but the trick doesn't seem to work.

I already have a workaround: define an interface, IMyInterface<T>, and then create my control as

public partial class MyControl : UserControl, IMyInterface<SomeClass> 

But I lose my base virtual methods (not a big deal, but still...).

Is there a way to create a base generic class for a UserControl, with the possiblity to edit it in the VisualStudio Designer?

like image 985
Filini Avatar asked Mar 24 '09 14:03

Filini


People also ask

What is UserControl in Winforms?

A UserControl is a collection of controls placed together to be used in a certain way. For example you can place a GroupBox that contains Textbox's, Checkboxes, etc. This is useful when you have to place the same group of controls on/in multiple forms or tabs.


1 Answers

We're doing the same thing and we work around by specializing a class first and derive from the specialized class. Using the code from your example this means something like:

public partial class UserControl : UserControlDesignable  {  ... } public class UserControlDesignable : BaseUserControl<Someclass> { } 

The designer is still acting flaky sometimes - but most of the time it works.

like image 88
bernhardrusch Avatar answered Sep 28 '22 17:09

bernhardrusch