EventHandlerList Question

  • Thread starter Thread starter CJ Taylor
  • Start date Start date
C

CJ Taylor

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
 
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
 
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
 
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
 
Back
Top