By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,266 Members | 1,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,266 IT Pros & Developers. It's quick & easy.

EventHandlerList Question

P: n/a
Hey,

I'm working with Dynamic Assemblies right now for a framework I'm building
and what I'm trying to understand is how EventHandlerList works. I've read
some documentaiton on it, but nothing really useful. Was wondering if
someone could shed some light on the subject.

Such as, if I declare an instance of an EventHandlerList and add handlers,
will it automatically know to grab the events when they are thrown?
Basically, is EventHandlerList.AddHandler = AddHandler keyword?

Thanks,
CJ
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
the "scoop" on eventhandlerlist. for all tense and purposes, the evl is
similar to a collection. it stores key/value pairs. it's a way to
consolidate the storage of object delegates. you can then use it to
determine what handler is actually handling any given delegate.

here's what i cooked up...hope it's what you're looking for:

'===========================================

Imports System.ComponentModel

Public Class frmMain

Inherits System.Windows.Forms.Form

#Region " variables "

Private eventList As New EventHandlerList()

#End Region

Private Sub formLoad(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'add to the list
eventList.AddHandler(Button1, New EventHandler(AddressOf buttonClick))
'fire event
Dim handler As EventHandler = eventList(Button1)
handler(Button1, EventArgs.Empty)
End Sub

Private Sub formClosing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
'remove from the list
eventList.RemoveHandler(Button1, New EventHandler(AddressOf buttonClick))
End Sub

Private Sub buttonClick(ByVal sender As Object, ByVal e As
System.EventArgs)
MsgBox("hello world!", Title:="sup cj?")
End Sub

End Class


Nov 20 '05 #2

P: n/a
Thanks a lot steve, very helpful.

I have a question for you. So, does this override in inherit event handler
list? Or when you use the AddHandler "keyword" is it basically creating all
this behind the scenes? Given what I've learned about .NET in the past
couple years, I would say "yes", but could be wrong.

Thanks,
CJ
"steve" <as*@abc.com> wrote in message
news:vm************@corp.supernews.com...
the "scoop" on eventhandlerlist. for all tense and purposes, the evl is
similar to a collection. it stores key/value pairs. it's a way to
consolidate the storage of object delegates. you can then use it to
determine what handler is actually handling any given delegate.

here's what i cooked up...hope it's what you're looking for:

'===========================================

Imports System.ComponentModel

Public Class frmMain

Inherits System.Windows.Forms.Form

#Region " variables "

Private eventList As New EventHandlerList()

#End Region

Private Sub formLoad(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'add to the list
eventList.AddHandler(Button1, New EventHandler(AddressOf buttonClick))
'fire event
Dim handler As EventHandler = eventList(Button1)
handler(Button1, EventArgs.Empty)
End Sub

Private Sub formClosing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
'remove from the list
eventList.RemoveHandler(Button1, New EventHandler(AddressOf buttonClick)) End Sub

Private Sub buttonClick(ByVal sender As Object, ByVal e As
System.EventArgs)
MsgBox("hello world!", Title:="sup cj?")
End Sub

End Class


Nov 20 '05 #3

P: n/a
i think i understand what you're asking...

using addhandler subscribes you to a particular event and runs you're
intended delegate code. if your object had tons of events defined w/n, you'd
create a bunch of overhead at run-time w/o the evl. the evl allows you to
assign the code to run when calling evl.addhandler but only creates the
appropriate delegate when a caller subscribes to it...only when needed
rather than setting them all when, really, only a few may be used. probably
clear as mud...

addhandler subscribes you to an object's event (your homemade object) and
your object is a good little fella w/ tons of events but, being a good
little fella, he's gunna just consume the resources he needs via an evl
rather than instanciating all event delegates on his creation.

hth,

steve
"CJ Taylor" <no****@blowgoats.com> wrote in message
news:OV**************@TK2MSFTNGP09.phx.gbl...
Thanks a lot steve, very helpful.

I have a question for you. So, does this override in inherit event handler list? Or when you use the AddHandler "keyword" is it basically creating all this behind the scenes? Given what I've learned about .NET in the past
couple years, I would say "yes", but could be wrong.

Thanks,
CJ
"steve" <as*@abc.com> wrote in message
news:vm************@corp.supernews.com...
the "scoop" on eventhandlerlist. for all tense and purposes, the evl is
similar to a collection. it stores key/value pairs. it's a way to
consolidate the storage of object delegates. you can then use it to
determine what handler is actually handling any given delegate.

here's what i cooked up...hope it's what you're looking for:

'===========================================

Imports System.ComponentModel

Public Class frmMain

Inherits System.Windows.Forms.Form

#Region " variables "

Private eventList As New EventHandlerList()

#End Region

Private Sub formLoad(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'add to the list
eventList.AddHandler(Button1, New EventHandler(AddressOf buttonClick))
'fire event
Dim handler As EventHandler = eventList(Button1)
handler(Button1, EventArgs.Empty)
End Sub

Private Sub formClosing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
'remove from the list
eventList.RemoveHandler(Button1, New EventHandler(AddressOf

buttonClick))
End Sub

Private Sub buttonClick(ByVal sender As Object, ByVal e As
System.EventArgs)
MsgBox("hello world!", Title:="sup cj?")
End Sub

End Class



Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.