form<T>

aronmatthew

Well-known member
Joined
Aug 5, 2019
Messages
86
Programming Experience
Beginner
I'm trying to create a genric windows form of type T with class
C#:
Expand Collapse Copy
public partial class form<T> : Form
and designer class
C#:
Expand Collapse Copy
partial class form<T>
although this builds without error the form tool designer is loading with error "The designer could not be shown for this file because none of the classes within it can be designed."
 
Are you married to using the GUI of the Forms Designer? You can hand code a form instead.
 
Forms Designer must be able to create an instance of the form to be designed. It can't provide any generic parameter, just like it needs a parameterless constructor because it also can't provide any constructor parameters. A suggestion is to have a base form that you design with GUI elements and base behaviour like internal GUI event handlers and such, then have a generic form that inherits from this and adds the generic code.
 
Forms Designer must be able to create an instance of the form to be designed. It can't provide any generic parameter, just like it needs a parameterless constructor because it also can't provide any constructor parameters. A suggestion is to have a base form that you design with GUI elements and base behaviour like internal GUI event handlers and such, then have a generic form that inherits from this and adds the generic code.
Ok so create a generic class that inherits from form... that seems possible vs just using object in this case.
 
As a reminder, the Form class derives from a Control. The Control has a Tag property is allowed to hold any object provided by the programmer. It's purpose is the same purpose as the Win32 API programming's GWLP_USERDATA or DWLP_USER where the Win32 API programmer and stash anything there, including pointers to control or dialog state information.

In other words, object is already built into the default Form. You just need to make use of it.
 
Last edited:
Ok so create a generic class that inherits from form... that seems possible vs just using object in this case.
Not generic in the <T> sense though, if that's what you mean. Basically, just create a form as you normally would, inheriting Form and then designed visually. You can then define a Form<T> class that inherits that. You won't be able to design that class but you won't need to.
 
Back
Top Bottom