Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inherit a generic form and open it in the Visual Studio designer?

In my application, I have a BaseForm which has a generic member in it:

public partial class BaseForm<T> : Form where T : Presenter
{
    protected T Presenter;

    public BaseForm()
    {
        InitializeComponent();
    }
}

Now what i need is to have a form which is inherited from my BaseForm

public partial class SampleForm : BaseForm<SamplePresenter>
{
    public SampleForm()
    {
        InitializeComponent();
        Presenter = new SamplePresenter();
    }
}

The problem is that the Visual Studio designer does not show my SampleForm derived from BaseForm<T>.

It gives this warning:

Warning 1 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:

SampleForm --- The base class 'Invoice.BaseForm' could not be loaded. Ensure the assembly has been referenced and that all projects have been built. 0 0

How can I overcome this?

P.S. I looked at this post but didn't really get the whole idea of how to solve this.

like image 501
Carmine Avatar asked Mar 30 '15 14:03

Carmine


1 Answers

The designer doesn't support this, as described in that post.

You need this base class:

public partial class SampleFormIntermediate : BaseForm<SamplePresenter>
{
    public SampleFormIntermediate()
    {
        InitializeComponent();
        Presenter = new SamplePresenter();
    }
}

And you need to use this class for the Visual Studio designer:

public partial class SampleForm : SampleFormIntermediate
{
}

In that way, Visual Studio 'understands' what to open in the designer and how to open it.

like image 102
Patrick Hofman Avatar answered Nov 11 '22 09:11

Patrick Hofman