Forms 表单继承应该是抽象的

Forms 表单继承应该是抽象的,forms,inheritance,overriding,abstract,Forms,Inheritance,Overriding,Abstract,这是我的密码 GeneralLesson.cs:它只是一个.cs文件,不是.Designer.cs或.resx文件 public /*abstract*/ class GeneralLesson : Form { public GeneralLesson() { this.StartPosition = SS.FrmStartPos;//If I want to change it later, I just change it here and all the

这是我的密码

GeneralLesson.cs:它只是一个.cs文件,不是.Designer.cs或.resx文件

public /*abstract*/ class GeneralLesson : Form
{
    public GeneralLesson()
    {
        this.StartPosition = SS.FrmStartPos;//If I want to change it later, I just change it here and all the children will get the change
    }
    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        base.OnFormClosing(e);
        if (e.CloseReason == CloseReason.WindowsShutDown) return;

        SS.LearningDay = 0; //SS is my static class that hold the static variables.
        SS.FrmMain.Show();
    }
}
public partial class _Sent_Lesson : GeneralLesson
{
    public _Sent_Lesson()
    {
        InitializeComponent();           
        richTextBox1.Text = "under construction";
    }

}
这实际上是一个windows窗体,带有.Designer.cs和.resx文件

public /*abstract*/ class GeneralLesson : Form
{
    public GeneralLesson()
    {
        this.StartPosition = SS.FrmStartPos;//If I want to change it later, I just change it here and all the children will get the change
    }
    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        base.OnFormClosing(e);
        if (e.CloseReason == CloseReason.WindowsShutDown) return;

        SS.LearningDay = 0; //SS is my static class that hold the static variables.
        SS.FrmMain.Show();
    }
}
public partial class _Sent_Lesson : GeneralLesson
{
    public _Sent_Lesson()
    {
        InitializeComponent();           
        richTextBox1.Text = "under construction";
    }

}
所以它实际上很符合我的目的。我的SentLesson窗口继承了GeneralLesson的构造函数和OnFormClosing。但问题是我无法再设计SentLesson窗口,它显示如下图所示:

也许我做错了。但是我不想创建GeneralLesson作为窗口,因为我没有对它设计任何控件,我只想重写一些函数,比如OnFormClosing或构造函数。我有没有办法不把Generallerson作为一个窗口来做这件事


谢谢阅读。

我终于找到了解决方案。尽管抽象父窗体在逻辑上仍然可以工作,但是子窗体不能用VisualStudioDesigner进一步设计。所以我必须以VisualStudio的方式继承表单。

正如您所看到的,VisualStudio为我们提供了继承的表单来创建子表单。因此,我将GeneralLesson.cs创建为Windows窗体,将SentLesson.cs创建为继承的窗体。因此,SentLesson继承了GeneralLesson的构造函数和OnFormClosing,并且SentLesson的设计器不再显示错误

如果希望子窗体可以从父窗体访问某些控件,例如父窗体的按钮A。只需在“属性”窗口中将父窗体的按钮A的修改器从“私有”设置为“受保护”。然后您可以使用子窗体中的按钮A执行任何操作


感谢您的阅读。

什么是SS?抛出异常的详细信息是什么?你的问题标题说应该是抽象的,但我在你的问题中没有看到任何抽象类型。请注意,抽象类型不能与WinForms designer一起使用。@Dai SS是我的静态类,包含静态变量。我在代码中对其进行注释。开始时,我将常规课程创建为公共抽象类GeneralLesson:Form,但后来我将其更改为公共类GeneralLesson:Form-不管怎样,还是一样,仍然会得到错误页面。抛出异常不是来自我的代码,它是因为VisualStudio无法加载设计器而导致的。