C# VB.net 委托 、事件、订阅 事件挂接匿名方法lambda 对比
C# VB.net 委托 、事件、订阅 事件挂接匿名方法lambda 对比
一、C#
private void butFormCount_Click(object sender, EventArgs e) { for (int i = 0; i < Convert .ToInt16 ( txbFormCount .Text ); i++) { Form2 form = new Form2(); form.Show(); form.TopMost=true ; form.one += () => { oneCount++; lab1Count.Text = oneCount.ToString(); }; form.two += () => { twoCount++; lab2Count.Text = twoCount.ToString(); }; form.three += () => { threeCount++; lab3Count.Text = threeCount.ToString(); }; } } private void Form2_Load(object sender, EventArgs e) { button1.Click += EndCount; but2.Click += EndCount; but3.Click += EndCount; }
二、VB.net
Private Sub butFormCount_Click(sender As Object, e As EventArgs) For i As Integer = 0 To Convert.ToInt16(txbFormCount.Text) - 1 Dim form As New Form2() form.Show() form.TopMost = True AddHandler form.one, Function() oneCount += 1 lab1Count.Text = oneCount.ToString() End Function AddHandler form.two, Function() twoCount += 1 lab2Count.Text = twoCount.ToString() End Function AddHandler form.three, Function() threeCount += 1 lab3Count.Text = threeCount.ToString() End Function Next End Sub Private Sub Form2_Load(sender As Object, e As EventArgs) AddHandler button1.Click, AddressOf EndCount AddHandler but2.Click, AddressOf EndCount AddHandler but3.Click, AddressOf EndCount End Sub
- 上一篇 »C#中的事件
- 下一篇 »可遇不可求的Question之C#中的匿名事件导致内存泄露的解决篇