4

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.

4
  • 1
    Can you give an example of when it would make sense to do this? Commented Jan 28, 2013 at 13:58
  • your custom control need to inherit the control class Commented Jan 28, 2013 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). Commented Jan 28, 2013 at 17:14
  • @Dave Bish updated the description. Hope it helps Commented Jan 28, 2013 at 17:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.