2015-10-01 55 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做任何事情。

感謝您的閱讀。