inheritance generic form cannot be shown in designer

前端 未结 2 875
南笙
南笙 2021-01-23 10:11

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.

相关标签:
2条回答
  • 2021-01-23 11:04

    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.

    0 讨论(0)
  • 2021-01-23 11:09

    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

    0 讨论(0)
提交回复
热议问题