//对事件的理解
//1 事件解决了通过“=”赋值将原来的事件替换的问题
//2 解决了在外部就可以写入方法的问题,疑问事件只能通过“+=或者-=”来赋值
//事件的本质
//1事件最终被编译器解析成一个私有的委托和两个public Add 和 Remove分别用来实现事件的+=和-=.
带参数的事件实例
用户控件代码 namespace 带参数的事件 { public delegate void loginDele(object sender,myEvent event1); public class myEvent { public string name { get; set; } public string pwd { get; set; } public bool log { get; set; } } public partial class ucLogin : UserControl { public event loginDele login; myEvent event1 = new myEvent(); public ucLogin() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { event1.log = false; event1.name = textBox1.Text.Trim(); event1.pwd = textBox2.Text; login(this, event1); if (event1.log) { textBox1.BackColor = Color.Green; textBox2.BackColor = Color.Green; } else { textBox1.BackColor = Color.Gray; textBox2.BackColor = Color.Gray; } } } } 窗体代码 private void Form1_Load(object sender, EventArgs e) { ucLogin1.login += ucLogin1_login; } void ucLogin1_login(object sender, myEvent event1) { if (event1.name == "aaa" && event1.pwd == "123") { event1.log = true; } else { event1.log = false; } }