Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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 :
VB
private abc as eventhandler
private sub MethodTest()
   addhandler me.abc, addressof Method1
End Sub



but i see some code do like below example 2:

example 2 :
VB
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


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.
Posted
Updated 29-Jan-12 13:57pm
v4
Comments
Sergey Alexandrovich Kryukov 18-Jan-12 4:30am    
???

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
 
Share this answer
 
This is what I have done in the past. I hope this helps you.

A separate class that does something.
VB
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.
VB
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.
 
Share this answer
 
v2
Comments
Idle_Force 19-Jan-12 13:41pm    
'Every time you call myFeedback.DoSomething() this Sub will be triggered.
Private Sub clsClassEvents_myFeedback (strMessage As String) Handles clsClassEvents.ReportFeedback '<-fixed this line, correct event name
Me.Text = strMessage
End Sub
Clark Kent123 19-Jan-12 13:48pm    
Thanks I have updated my solution.
jonaschu 29-Jan-12 19:55pm    
bro, thanks for your suggestion. i know for this method.. this method normally i use as a static event handler.. i just don't know what is the different from example 1 and example 2 code i paste on the top.
Clark Kent123 30-Jan-12 13:56pm    
No problem. I encourage you to check the link Simon_Whale posted (Solution #2). The answer is found there.

Check the section on "Associating Events with Event Handlers"
http://msdn.microsoft.com/en-us/library/2z7x8ys3%28v=vs.71%29.aspx

Finally, I would like to encourage you to vote if either or both of us have answered your question. So it can be closed or rephrase your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900