Form2操作FORM1的方法 (委托方法)

public class MsgEventArgs:System.EventArgs//必须从这个类继承
{
public string textMsg;
public MsgEventArgs()
{}
}
public delegate void MyHandler(object sender,MsgEventArgs e);

public class Form2 : System.Windows.Forms.Form
{
public event MyHandler MyEvent;

private void button1_Click(object sender, System.EventArgs e)
{
if (MyEvent != null)
{
MsgEventArgs ea=new MsgEventArgs ();
ea.textMsg=this.textBox1.Text; //假设From2上有个textBox1
MyEvent(this,ea);
}
}
}

public class Form1 : System.Windows.Forms.Form
{
private void button1_Click(object sender, System.EventArgs e)
{
Form2 form2 = new Form2();
form2.MyEvent += new MyHandler(form2_MyEvent);
form2.Show();
}

private void form2_MyEvent(object sender,MsgEventArgs e)
{
//statusBar1.Text = "Form2 Button Clicked!";
statusBar1.Text = e.textMsg;
}
}
这样就把From2中TextBox的属性带回来了

 

你可能感兴趣的:(Form2操作FORM1的方法 (委托方法))