3

We wish to create a WinForms Custom Control (which is derived from a .net control) and be able to drag and drop it from the tool box in the designer view. However we are unable to this whenever we have a control with generics because when the designer tries to create an instance of the class, for obvious reasons, it doesn't know what type the instance must be of.

Any one knows of a way around this ?

Thanks in advance

======edit===========

What we would like to do is have example:

public class MyDataGridView<T> : DataGridView where T : class{
...
}

by removing the generics we can see the preview in the designer as if it where a normal DataGridView but as I explained above with generics we are unable to use the designer.

Jonny
  • 2,787
  • 10
  • 40
  • 62
  • 1
    Can you give an example of when it would make sense to do this? – Dave Bish Jan 28 '13 at 13:58
  • your custom control need to inherit the control class – Elior Jan 28 '13 at 13:58
  • An unfortunate shortcoming of the designer that I've struggled with over the years. The ugly workaround that I ended up adopting was using an empty, non-generic class by the same name to design the control. My generic class reuses the same `InitializeComponent`, and I simply switch between the two classes manually (commenting one out). – JosephHirn Jan 28 '13 at 17:14
  • @Dave Bish updated the description. Hope it helps – Jonny Jan 28 '13 at 17:30

1 Answers1

3

I have faced the same problem and the upshot was: Don't use generic controls in the designer view! You can programatically create instances of it, but you have no preview. Maybe it works when you add a derived class which explicitly sets the generic parameter.

Peter Stock
  • 301
  • 1
  • 7
  • I've heard this works. Some older posts claim that in addition to deriving from the generic class, you had to derive from the derived class as well. I have no idea if that has since been fixed, but tripling the number of forms kept me away from ever trying it. – JosephHirn Jan 28 '13 at 17:16
  • We had the same problem and the same solution and ended up by putting a container (e.g. a panel control) as a placeholder into the form. Then we would create programmaticaly create the control as a child of the container and set it to DockStyle.Fill. – Stephan Keller Jan 28 '13 at 18:25