Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms Generic Inheritance

I have these classes:

class Foo<T1, T2> : Form 
    where T1, T2 : EventArgs

class MiddleGoo : Foo<X,Y>

class Goo : MiddleGoo

X,Y are just simple classes derived from EventArgs.

I see Goo in designer, but i want to create a class Boo between Foo and Goo like this:

class Boo<T1, Y> : Foo<T1, Y>
where T1 : EventArgs

class MiddleGoo : Boo<X,Y>

class Goo : MiddleGoo

Workaround with middle class doesn't work, any ideas?

EDIT: I meant Y and X are classes like YEventArgs and XEventArgs and my problem is about seeing in designer class Boo when i defined Y as T2 but still want to keep it generic through T1.

EDIT2: I just realized I misspelled something about Y class...

public class Foo<T1, T2> : Form
    where T1 : EventArgs
    where T2 : EventArgs
{
}

public class Boo<T1> : Foo<T1, MyEventArgs2>
    where T1 : EventArgs
{
}

public class MiddleGoo : Boo<MyEventArgs1>
{
}

class Goo : MiddleGoo
{
}

public class MyEventArgs2 : EventArgs
{
}

public class MyEventArgs1 : EventArgs
{      
}

And to be clear I just can't see Boo in Designer... ( I can't see MiddleGoo too but i don't need to)

like image 587
Plondrein Avatar asked Oct 13 '15 21:10

Plondrein


People also ask

How do I inherit Windows Forms?

In order to inherit from a form, the file or namespace containing that form must have been built into an executable file or DLL. To build the project, choose Build from the Build menu. Also, a reference to the namespace must be added to the class inheriting the form.

Can a generic class inherit?

You cannot inherit a generic type. // class Derived20 : T {}// NO!

Will Winforms be deprecated?

WinForms won't be deprecated until Win32 is ... which could be quite sometime! WPF on the other hand has few direct dependencies on Win32 so could potentially form the basis of a "fresh start" UI layer on a future version of windows.


1 Answers

For Visual Studio Version >= VS2015.1

Starting from VS2015.1, Windows Forms Designer shows classes which have generic base classe without any problem. So the workaround which is in other posts is no more required for newer versions of VS and the following class will be shown in designer without any problem.

So having a base generic class like this:

public class BaseForm<TModel,TService> : Form
{
    public TModel Model {get;set;}
    public TService Service {get; set;}
}

You can create the derived form without any problem in designer:

public class FooForm: BaseForm<Foo,FooService> 
{
}

Older Versions of Visual Studio

In older versions of Visual Studio, when the designer wants to host your form in designer, it tries to create an instance of base class of your form, and your class must have a non-generic base to so the designer can show it.

So you can see BaseForm<T>:Form can be shown in designer but CategoryForm:BaseForm<Category> can not be shown in designer. As a workaround in these cases you should create a BaseCategoryForm:BaseForm<Category> and then CategoryForm:BaseCategoryForm will be shown in designer.

Example

Suppose this is your base class that accepts TModel as Model and TService as Service for example:

public class BaseForm<TModel,TService> : Form
{
    public TModel Model {get;set;}
    public TService Service {get; set;}
}

Then create an intermediate form this way, with this line of code:

Public Class BaseFooForm: BaseForm<Foo, FooService>{ }

And the final form this way:

public class FooForm: BaseFooForm
{
}

Now the final FooForm has designer and you can work with it normally. This way you can create your classes to be supported in designer.

Note

The update is also applied on control designer. So also in generic base class for WinForm UserControl you no more need such workaround for VS>=VS2015.1 .

like image 73
Reza Aghaei Avatar answered Sep 21 '22 16:09

Reza Aghaei