多种形式没有出现

时间:2013-01-19 17:27:00

标签: c# winforms

我正在尝试制作multiWindowsForm

为了尝试它是如何工作的,我开始使用一个简单的表单,我添加了一个按钮。点击它时,会弹出另一个窗口。但我无法让它发挥作用。它崩溃了,错误:

Object reference not set to an instance of an object!

我使用 Project 添加 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 个答案:

答案 0 :(得分:4)

看起来load事件没有触发,因此不会启动你的对象。确保连接了load事件。

或者,在点击事件中初始化。

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

我希望这会有所帮助。

答案 1 :(得分:2)

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