1

I meet a problem today. As following.
I create a generic Form ,
public class Form1:Form
Then I create another inheritance form,
public class From2:Form1.
The form2 cannot be shown in the VS designer, the error message is "all the classes in the file cannot be designed", (this error message is translated from Chinese, the Chinese message is 文件中的类都不能进行设计).
But this program can be compiled successfully, and when it runs, both Form1 and From2 can work.

Anyone can give me some help about this ? Thanks.

I am not a native English speaker, I hope I have described my question clear.

Jerry
  • 435
  • 4
  • 12

2 Answers2

6

This is a limitation with the designer. You can work around it by adding an interim derived form that specifies the types. I've explained this in a blog post:

http://adamhouldsworth.blogspot.com/2010/02/winforms-visual-inheritance-limitations.html

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
  • Adam, thank you. I have read your article. The article discusses the very same question as mine. I have found a solution according to your article, and it works well. Thanks. I want to accept your answer as the correct or best one, but I don't find any links or buttons to do so. What should I do? – Jerry Jan 04 '11 at 12:30
  • @Jerry don't worry about it, so long as your problem is solved that's fine by me :-) – Adam Houldsworth Jan 04 '11 at 12:35
  • I have also met the second problem descussed in your article: the DataGridView cannot be designed in the inheritance form. Once again, your article gives the correct answer. And I found the button to accept your answer. Thanks again. – Jerry Jan 06 '11 at 06:55
2

I had like to give a more concrete answer on as addition to the post of Adam.

BaseForm is the name of your generic form, GenericClass one of the possible type parameters.

BaseForm<T> could look like this:

public class BaseForm<T> : Form
{ }

First, you need the above base class. In fact this is the part you'd probably already used before bumping into this question.

Then you use this intermediate implementation.

public class SampleFormIntermediate : BaseForm<GenericClass>
{
    public SampleFormIntermediate()
    {
        InitializeComponent();
    }
}

And you need to use this class for the Visual Studio designer. And only that. I would recommend to decorate this with a compiler directive so it only gets used in Debug mode:

public partial class SampleForm : SampleFormIntermediate
{
}

Using this Visual Studio 'understands' what to open in the designer and how to open it.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325