5.委托+事件的方法
下面代码是一个点击Form1 button 使Form2的button显示Form1.textbox内容
可以一次性传很多值,步骤是在窗体A中声明一个事件,B窗体中实现相同方法签名的方法为事件赋值,B中回调该方法
Form1的代码:
public partial class Form1 : Form{
public string B //获取textbox1的text
{
get { return textBox1.Text; }
set
{
textBox1.Text = value;
}
}
public delegate void EventArgsaccept(object sender, acceptEventArgs e);//声明一个事件签名的委托
public static event EventArgsaccept accept;//相当于实例化一个事件
private void button1_Click(object sender, EventArgs e)
{
acceptEventArgs ae = new acceptEventArgs();
ae.b = B;
if (accept != null) {
accept(this,ae);
}
}
}
}
public class acceptEventArgs : EventArgs {//封装EventArgs类,添加可传递的属性
public string b { get; set; }
}
//------------------->>----------------------------------end code of form1-----------
form2的代码,实现一个相同签名的方法,如我们的accept的签名是 方法名(object a,acceptEventArgs b);
复制代码 //-------------------------------------->>---------------------------------------code of form2---
public partial class Form2 : Form{
private void Form2_Load(object sender, EventArgs e)










