After working on a windows forms project I realized many of my screens do very similar tasks on completely different types of data. I then decided to create a generics based user control to make reuse of all of this common work based on the Type of object the specific instance of the control is using.
After I build my control I realized I no longer get any design-time support for my class. It doesn't even show up as a user control available in the tool bar. This makes sense, since an instance of a generic type needs the type defined in order for it to be rendered on the screen, however is there any way I can add this generic type to my windows form and specify its type and then be able to use the design-time support of modifying this control
Example:
public class ItemView<T> : UserControl
{
protected T item;
public T Item{get{return item;}set{item=value;}}
}

Generic User Controls?
su45937
This is a really good question... I haven't been playing much in the ASP.NET world for a few months, so if you figure something out please let me know.
Anyone else have any ideas here
trc_pdx
Jonathan MacCollum,
Decision suggested by John.Doe's it not really alternative. Because it kill Idea of Genereic without a trace. I (NOT COMPILER) must create many classes what realise my templates.
And another moment i put on control in InitializeComponent block some concrete class SomeControl<Strring>. But when i did it Designer is no longer represent myControl, although it's work.
nanceoiage
Prabu.
FlatBread
Thanks a lot, your response has lead me into a good direction, however after tinkering around with this idea here's a similar problem I've come into...
I've created my templated user control (ListItem<T> : UserControl) and created a new UserControl DecimalListItem and changed in code the inherited class from UserControl to ListItem<decimal>.
My new DecimalListItem shows up in the toolbar and is visible in the designer when I drag this onto my form and contains all of the information available to me from the ListItem<T> class that I wanted it to inherit. However when I go back to the DecimalListItem control in design view I get a VS Design time error:
The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: DecimalListItem--- The base class 'ListItem' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.
Perhaps this is because the designer can not create a visual representation of the base class ListItem<T> even though the DecimalListItem declares the type of T to be decimal.
Any idea why it will let me see it in my Form1 designer view and not in the DecimalListItem designer view
A. Lan
The designer is not able to create an instance of the control because of the generic type, it detects that and therefore it does not appear in the toolbox.
The only solution I would see is that you build a non-generic wrapper control arround your generic control and add one additional property "ControlType". then you add a TypeBrowser like editing control for the property and whenever the control type changes you recreate the wrapped generic control.
adiel
Yeti32
John, Yup, that works.
As for your example its interesting how Control1<T> : UserControl{...} works in the designer, Control2:Control1<double>{...} doesn't and Control3:Control2{...} does.
Makes me wish I knew more about the visual studio designer when it comes to loading types.
Thanks for your assistance on this.
Paul Stubbs - MSFT
I would consider this a bug...
Funny thing is, if you wrap it twice:
public class Control1<T> : UserControl { ... }
public class Control2 : Control1<double> { ... }
public class Control3 : Control2 { ... }
Then Control3 works perfectly ;)
I guess not all parts of the designer are yet capable of handling generics.
Jim Ashton
When I need the desigh time,I simply make it inherited from UserControl...
When I compiling the project,change it back.
Seems stupid,but it really works.
Pete2
btimur,
This was resolved in John.Doe's posts marked as 'answered' above. This actually makes a lot of sense too, because one would need to know the 'type' of the templated object in order to make an instance of it.
More design-time support for creating a New Control, or New Windows Form from a templated class would be nice through the use of a wizard (say that lets you select the data types used) would prove quite useful.
However for now, just create the templated type, inherit from it with the type you want your new form to be. And create a new form/control that inherits from this typed instance of your template.
Bit_Flipper
At least I did not report it, I am not sure anyway if this REALLY can be considered a bug.
As stated above already you cannot instantiate a generic class directly, so the designer is obviously unable to show you an instance in design mode of the control. Also assuming that for design purpose a new feature will be developed to create/show a designable instance of a generic class would it make sense What happens if you have public properties which use the generic type, how would the designer know what kind of editor to display
So, in my opinion that is just the way it is, the designer is just a convenient tool and in cases like this you can/must live without it.