标签:
namespace 使用委托窗体传值
{ public partial class Form1 : Form // 窗体1 { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // 创建Form2实例f2 Form2 f2 = new Form2(); // 向PassValue事件订阅一个事件,该事件源会携带一个带textbox2的信息 f2.PassValue+=new Form2.SetTextToLabelHandler(Form2_ButtonClicked); f2.Show(); } private void Form2_ButtonClicked(object send, SetTextToLabelEventArgs e) { label1.Text = e.Text; } } }namespace 使用委托窗体传值{ public partial class Form2 : Form // 窗体2 { public Form2() { InitializeComponent(); } // 声明传值委托 public delegate void SetTextToLabelHandler(object sender, SetTextToLabelEventArgs e); // 声明一个传值委托类型的事件 public event SetTextToLabelHandler PassValue; private void button1_Click(object sender, EventArgs e) { // 创建事件源类实例并传递textbox1的值过去 SetTextToLabelEventArgs args = new SetTextToLabelEventArgs(textBox1.Text); PassValue(this, args); // 触发事件 this.Dispose(); } } public class SetTextToLabelEventArgs : System.EventArgs { public string Text { get; set;} public SetTextToLabelEventArgs(string text) { this.Text= text; } }}标签:
原文地址:http://www.cnblogs.com/zxws1009/p/4176097.html