事件处理程序和委托 [英] Event Handler and Delegate

查看:75
本文介绍了事件处理程序和委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我对事件处理程序有疑问,我在此事件处理程序/委托中确实很弱.
正常的事件处理程序,例如示例1,对我来说还是可以的,

示例1:

Hello all,

i have a question for the event handler, i''m really weak in this event handler/delegate.
normal event handler like example 1 still ok for me,

example 1 :

private abc as eventhandler
private sub MethodTest()
   addhandler me.abc, addressof Method1
End Sub




但是我看到一些代码像下面的示例2一样:

例子2:




but i see some code do like below example 2:

example 2 :

Private ZZZ As System.EventHandler = Nothing

private sub Method999()
   Me.ZZZ = New EventHandler(AddressOf Method1)
   <big>AddHandler me.AnotherEvent, me.ZZZ</big>
End Sub

private sub Method1(byval sender as object, byval e as eventArgs)
   'do something
End Sub



据我所知,示例1和示例2将执行相同的功能.
我不太了解如何使用代码.
希望有人为此提供帮助.



as what i know the example 1 and example 2 will perform the same function.
im not really understand how to use the code do like this.
hope somebody help me for this.

推荐答案

MSDN: EventHandler Delegate[^]

From reading the above article there is no technical difference between them except you are creating a eventhandler variable and assigning the addressof statement to that rather than the addhandler statement.

hope that makes sense


这是我过去所做的.希望对您有帮助.

一个单独的类,可以执行某些操作.
This is what I have done in the past. I hope this helps you.

A separate class that does something.
Public Class clsClassEvents
Event ReportFeedback(strMessage As String)

Public Sub DoSomething()
RaiseEvent ReportFeedback("I did something!")
End Sub
End Class



在我的主要形式中,我声明了我的课程,如下所示.



In my main form I declared my class as follows.

Private WithEvents myFeedback As New clsClassEvents()

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
myFeedback.DoSomething()
End Sub

'Every time you call myFeedback.DoSomething() this Sub will be triggered.
Private Sub myFeedback_ReportFeedback(strMessage As String) Handles myFeedback.ReportFeedback
Me.Text = strMessage
End Sub



让我知道是否有帮助.



Let me know if this helps.


这篇关于事件处理程序和委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆