2015-10-01 49 views
0

这里我的代码表继承应该是抽象的

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:这实际上是一个窗口形式,具有了.Designer.cs和.resx文件

public partial class _Sent_Lesson : GeneralLesson 
{ 
    public _Sent_Lesson() 
    { 
     InitializeComponent();   
     richTextBox1.Text = "under construction"; 
    } 

} 

所以它实际上成为我的目的很好。我的SentLesson窗口继承了GeneralLesson的构造函数和OnFormClosing。但问题是我无法再设计我的SentLesson窗口,它显示如下图: Object reference not set to an instance of an object.

也许我做错了。但我不想创建GeneralLesson作为窗口,因为我没有设计任何控件,我只是想重写一些函数,如OnFormClosing或构造函数。有没有什么办法可以做到这一点,而不需要将GeneralLesson作为一个窗口。

感谢您的阅读。

+0

'SS'是什么?抛出异常的细节是什么?你的问题标题是“应该是抽象的”,但在你的问题中我没有看到任何“抽象”类型。请注意,'抽象'类型不能用于WinForms设计器。 – Dai

+0

@Dai SS是我的静态类,它包含静态变量。我在我的代码中评论它。 开始时,我将普通课程创建为“公共抽象类GeneralLesson:Form”,但之后我将其更改为“公共类GeneralLesson:Form” - 同样的事情,仍然得到Error页面。并且抛出的异常不是来自我的代码,而是因为Visual Studio无法加载Designer。 – 123iamking

回答

0

我终于找到了解决办法。尽管抽象父窗体仍然在逻辑上工作,但是Visual Studio设计人员无法再设计子窗体。所以我必须继承窗体的Visual Studio的方式。如您所见,Visual Studio为我们提供了继承形式来创建子窗体。所以我创建了GeneralLesson.cs作为Windows Form,并将SentLesson.cs创建为Inherited Form。所以SentLesson继承了GeneralLesson的构造函数和OnFormClosing,并且SentLesson的设计者不再显示错误。

如果您希望Children窗体可以从Parent窗体访问某些控件,请说Parent的“Button A”。只需将属性窗口中父母的“按钮A”的修改器从“私人”设置为“受保护”即可。然后,您可以使用“儿童”表单中的按钮A做任何事情。

感谢您的阅读。