tags:

views:

1034

answers:

3

You should be able to create a generic form:

public partial class MyGenericForm<T> :
    Form where T : class
{
    /* form code */
    public List<T> TypedList { get; set; }
}

Is valid C#, and compiles. However the designer won't work and the form will throw a runtime exception if you have any images stating that it cannot find the resource.

I think this is because the windows forms designer assumes that the resources will be stored under the simple type's name.

A: 

I have a hack to workaround this, which works but isn't ideal:

Add a new class to the project that inherits the form with its simple name.

internal class MyGenericForm:
    MyGenericForm<object> { }

This means that although the designer is still wrong the expected simple type (i.e without <>) is still found.

Keith
+4  A: 

Yes you can! Here's a blog post I made a while ago with the trick:

Designing Generic Forms

Edit: Looks like you're already doing it this way. This method works fine so I wouldn't consider it too hacky.

Matt Hamilton
What about if you have 3 forms? I want Form3 : Form2. This includes the generic type parameter + controls in Form2. Any idea?
mynkow
A: 

Thanks @Matt Hamilton - your post is more detailed than mine :-)

It's basically the same workaround though, is there a way to do this without the hack?

Keith