一段简单有关事件代码的错误

来源:百度知道 编辑:UC知道 时间:2024/06/22 03:02:38
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
button1.Click += new EventHandler(button1_Click);//错误:未将对象引用设置到对象的实例。
InitializeComponent();
}

void button1_Click(object sender, EventArgs e)
{
throw new Exception("The method or operation is not implemented.");
}
}
}
谢谢网络剑客Eks的回答。其实上面的异常的代码是vs2005自动生成的,我开始的时候也是以为这个地方有问题,但是改了之后还是一样的错误。

button1.Click += new EventHandler(button1_Click);
你调用了void button1_Click这个方法
而在方法体里面抛了个异常。

throw new Exception("The method or operation is not implemented.");
这行删掉就可以了
写你想要运行的代码

----------------------------

请将
InitializeComponent();
button1.Click += new EventHandler(button1_Click);
两行代码换一下位置。
InitializeComponent()
这个方法是初始化控件。所以要放在前面。