表单继承应该是抽象的
Form inheritance should be 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 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();
}
}
SentLesson.cs:这实际上是一个windows表格,带有.Designer.cs和.resx文件
public partial class _Sent_Lesson : GeneralLesson
{
public _Sent_Lesson()
{
InitializeComponent();
richTextBox1.Text = "under construction";
}
}
所以它实际上很符合我的目的。我的 SentLesson window 从 GeneralLesson 继承了构造函数和 OnFormClosing。但问题是我不能再设计我的 SentLesson window 了,它显示如下图:
也许我做错了。但我不想将 GeneralLesson 创建为 Window,因为我没有在其上设计任何控件,我只想重写一些函数,如 OnFormClosing 或 Constructor。有什么办法可以做到这一点,而无需将 GeneralLesson 作为 Window.
感谢阅读。
我终于找到了解决办法。尽管抽象的父窗体在逻辑上仍然有效,但子窗体不能用 Visual Studio 设计器进一步设计。所以我必须以 Visual Studio 的方式继承表格。
如您所见,Visual Studio为我们提供了Inherited Form来创建Children form。因此,我将 GeneralLesson.cs 创建为 Windows 表单,将 SentLesson.cs 创建为继承表单。所以SentLesson继承了GeneralLesson的constructor和OnFormClosing,SentLesson的设计器不再报错
如果您希望子窗体可以从父窗体访问某些控件,例如父窗体 "Button A"。只需将属性 window 中父项的 "Button A" 的修饰符从私有设置为受保护。然后您可以使用子窗体中的按钮 A 执行任何操作。
感谢您的阅读。
这是我的代码
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();
}
}
SentLesson.cs:这实际上是一个windows表格,带有.Designer.cs和.resx文件
public partial class _Sent_Lesson : GeneralLesson
{
public _Sent_Lesson()
{
InitializeComponent();
richTextBox1.Text = "under construction";
}
}
所以它实际上很符合我的目的。我的 SentLesson window 从 GeneralLesson 继承了构造函数和 OnFormClosing。但问题是我不能再设计我的 SentLesson window 了,它显示如下图:
也许我做错了。但我不想将 GeneralLesson 创建为 Window,因为我没有在其上设计任何控件,我只想重写一些函数,如 OnFormClosing 或 Constructor。有什么办法可以做到这一点,而无需将 GeneralLesson 作为 Window.
感谢阅读。
我终于找到了解决办法。尽管抽象的父窗体在逻辑上仍然有效,但子窗体不能用 Visual Studio 设计器进一步设计。所以我必须以 Visual Studio 的方式继承表格。
如您所见,Visual Studio为我们提供了Inherited Form来创建Children form。因此,我将 GeneralLesson.cs 创建为 Windows 表单,将 SentLesson.cs 创建为继承表单。所以SentLesson继承了GeneralLesson的constructor和OnFormClosing,SentLesson的设计器不再报错
如果您希望子窗体可以从父窗体访问某些控件,例如父窗体 "Button A"。只需将属性 window 中父项的 "Button A" 的修饰符从私有设置为受保护。然后您可以使用子窗体中的按钮 A 执行任何操作。
感谢您的阅读。