2013-01-19 66 views
2

我正在嘗試創建一個multiWindowsForm多個表單不顯示

只是嘗試它是如何工作的,我開始使用一個簡單的表單,我添加了一個按鈕。點擊它時,會彈出另一個窗口。但我無法讓它工作。它與錯誤崩潰:

Object reference not set to an instance of an object! 

我用項目添加Windows窗體並把它命名爲Mupp.cs

這裏是我的Form1.cs代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace MultiForm 
{ 
    public partial class tryout : Form 
    { 
     public tryout() 
     { 
      InitializeComponent(); 
     } 

     Mupp theMupp; 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      theMupp = new Mupp(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      theMupp.Show(); 
     } 
    } 
} 

我能錯過了嗎?

+2

發佈您的Mupp類的代碼。另外,它會在'theMupp.Show()'或Mupp類本身的某個地方崩潰嗎? –

+0

當你新增你的Mupp類時,你可能會遇到問題。我們需要看代碼。 –

+0

我沒有用Mupp.cs代碼編輯帖子。謝謝!〜 –

回答

4

它看起來像加載事件不會觸發,因此不能初始化您的對象。確保加載事件已連接。

或者,在點擊事件中進行初始化。

private void button1_Click(object sender, EventArgs e) 
{ 
    using (Mupp theMupp = new Mupp()) 
    { 
     theMupp.ShowDialog(); 
    } 
} 

我希望這有助於。

2
public tryout() 
{ 
     InitializeComponent(); 
     this.Load += new EventHandler(Form1_Load); 
}