c#委托与事件编程nullreference错误
想把form2的picturebox图形添加到form1的picturebox,使用委托,但出现nullreference的错误,求大神指教。
form2代码:
public delegate void MyDelegate(Image i);
public event MyDelegate MyEvent;
按钮的click事件中:MyEvent(this.pictureBox1.Image);
form1代码:
Form2 f2 = new Form2();
f2.MyEvent += new f2.MyDelegate(f2_MyEvent);
void f2_MyEvent(Image i)
{
this.pictureBox1.Image = i;
}
本人刚学委托与事件,望大神不吝赐教,谢谢。
------解决方案--------------------form2
public delegate void MyDelegate(Image i);
public event MyDelegate MyEvent;
click:
if(MyEvent!=null) MyEvent(this.pictureBox1.Image);
form1
click:
Form2 f2 = new Form2();
f2.MyEvent += new Form2.MyDelegate(f2_MyEvent);
f2.Show();
void f2_MyEvent(Image i)
{
this.pictureBox1.Image = i;
}