7

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.

Community
  • 1
  • 1
vcmkrtchyan
  • 2,536
  • 5
  • 30
  • 59

1 Answers1

10

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.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Isn't the idea to have different presenters in different inheriting classes. – Magnus Mar 30 '15 at 14:52
  • @Magnus: OPs problem focusses on the Visual Studio designer integration. Nothing more. I think this is what it should be for that. – Patrick Hofman Mar 30 '15 at 14:52
  • Thanks, that's exactly what I was looking for. Now I can just have a test package where I'll be able to open the UI with the designer, and have the actual project implemented via generics. Thanks again:) – vcmkrtchyan Mar 30 '15 at 16:26
  • @Magnus can you also provide a documentation or some other related links from which you have come up to the solution? The solution worked for me, now i'm wondering about actually why does the designer work that way. – vcmkrtchyan Mar 31 '15 at 11:00
  • This answer is very useful, especially considering that this workaround is definetely counterintuitive. Thanks. – Jack Griffin Apr 26 '15 at 13:00