473,748 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

EventHandlerLis t question (1.1)

I'm having a play with EventHandlerLis t but the documentation is a bit
ropey and I can't find any decent examples. It also doesn't seem to do
what I was led to believe it would. I was under the impression that
windows.forms controls used EventHandlerLis ts because generally most
events aren't consumed so this saves memory.
I can add a button to a form, and have

this.button9.Cl ick += new System.EventHan dler(this.butto n9_Click);
this.button9.Cl ick += new System.EventHan dler(this.butto n9b_Click);

which will successfully fire off both functions when the button is
clicked.

The EventHandlerLis t does not fire both as it keys the delegate in a
hashtable and overwrites the first reference with the second reference
(This is using AddHandler).
More confusingly in trying to get the thing running I see code all over
the internet that can't run. A VB example that used a foreach (there's
no enumerator so that can't work) and a C++ example where the
EventHandlerLis t returns an EventHandler (It only returns Delegates in
my tests)
I'm certain I'm right, I can see plenty of alternative ways of doing it
that would work, I'm just confused about the information I'm getting
off the internet.
So am I being fed porkies? Do Windows Forms controls use
EventHandlerLis t objects and if so how come their implementation allows
for += and the one in ComponentModel doesn't?

Thanks
Ian

Nov 8 '06 #1
4 4218
Hi Ian,

<snip>
I was under the impression that
windows.forms controls used EventHandlerLis ts because generally most
events aren't consumed so this saves memory.
True.
I can add a button to a form, and have

this.button9.Cl ick += new System.EventHan dler(this.butto n9_Click);
this.button9.Cl ick += new System.EventHan dler(this.butto n9b_Click);

which will successfully fire off both functions when the button is
clicked.

The EventHandlerLis t does not fire both as it keys the delegate in a
hashtable and overwrites the first reference with the second reference
(This is using AddHandler).
Here's an example that I'm using from a project I'm working on right now in a
class that derives from Component:

private readonly object ServiceViewChan gedEvent = new object();

public event EventHandler<Se rviceViewChange dEventArgsServi ceViewChanged
{
add
{
lock (ServiceViewCha ngedEvent)
{
Events.AddHandl er(ServiceViewC hangedEvent, value);
}
}
remove
{
lock (ServiceViewCha ngedEvent)
{
Events.RemoveHa ndler(ServiceVi ewChangedEvent, value);
}
}
}

private void OnServiceViewCh anged(ServiceVi ewChangedEventA rgs e)
{
EventHandler<Se rviceViewChange dEventArgshandl er = null;

lock (ServiceViewCha ngedEvent)
{
handler = (EventHandler<S erviceViewChang edEventArgs>)
Events[ServiceViewChan gedEvent];
}

if (handler != null)
handler(this, e);
}
More confusingly in trying to get the thing running I see code all over
the internet that can't run. A VB example that used a foreach (there's
no enumerator so that can't work) and a C++ example where the
EventHandlerLis t returns an EventHandler (It only returns Delegates in
my tests)
EventHandler is a delegate. The value returned by the EventHandlerLis t
indexer isn't type-safe, so it must be cast into the appropriate delegate
Type, as you can see in the code above.
I'm certain I'm right, I can see plenty of alternative ways of doing it
that would work, I'm just confused about the information I'm getting
off the internet.
So am I being fed porkies? Do Windows Forms controls use
EventHandlerLis t objects and if so how come their implementation allows
for += and the one in ComponentModel doesn't?
An EventHandlerLis t instance is exposed as a protected property named,
"Events" from the Component class to all derived types. The "+=" operator
acts on public events, not the "Events" property. The underlying
implementation of "add" and "remove" on these public events can use the
"Events" property, internally, to store the supplied delegate.

Yes, I'm sure WinForms controls use the Events property, though I'm not sure
if ALL events on ALL WinForms Controls are registered through the inherited
Component.Event s property. Web controls use the "Events" property as well,
but it's declared in the base Control since Control doesn't derive from
Component. It still works the same way.

In my example, the public event is "ServiceViewCha nged". The "add" and
"remove" implementations of that event, which correspond to the "+=" and "-="
operators, respectively, add or remove the supplied delegate to or from the
underlying EventHandlerLis t by calling either Events.AddHandl er or
Events.RemoveHa ndler. Both of these methods require the same key used in the
indexer when you attempt to retrieve the delegate reference from the list.

If things still aren't clear, let me know.

--
Dave Sexton
Nov 8 '06 #2
Hi Ian,

Sorry about using C# generics - I just realized you wrote (1.1) in the title.

To be honest, I don't have a code snippet without generics on hand anyway. If
you need me to rewrite my example without generics I will - just let me know.

--
Dave Sexton

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
Hi Ian,

<snip>
>I was under the impression that
windows.form s controls used EventHandlerLis ts because generally most
events aren't consumed so this saves memory.

True.
>I can add a button to a form, and have

this.button9.C lick += new System.EventHan dler(this.butto n9_Click);
this.button9.C lick += new System.EventHan dler(this.butto n9b_Click);

which will successfully fire off both functions when the button is
clicked.

The EventHandlerLis t does not fire both as it keys the delegate in a
hashtable and overwrites the first reference with the second reference
(This is using AddHandler).

Here's an example that I'm using from a project I'm working on right now in
a class that derives from Component:

private readonly object ServiceViewChan gedEvent = new object();

public event EventHandler<Se rviceViewChange dEventArgsServi ceViewChanged
{
add
{
lock (ServiceViewCha ngedEvent)
{
Events.AddHandl er(ServiceViewC hangedEvent, value);
}
}
remove
{
lock (ServiceViewCha ngedEvent)
{
Events.RemoveHa ndler(ServiceVi ewChangedEvent, value);
}
}
}

private void OnServiceViewCh anged(ServiceVi ewChangedEventA rgs e)
{
EventHandler<Se rviceViewChange dEventArgshandl er = null;

lock (ServiceViewCha ngedEvent)
{
handler = (EventHandler<S erviceViewChang edEventArgs>)
Events[ServiceViewChan gedEvent];
}

if (handler != null)
handler(this, e);
}
>More confusingly in trying to get the thing running I see code all over
the internet that can't run. A VB example that used a foreach (there's
no enumerator so that can't work) and a C++ example where the
EventHandlerLi st returns an EventHandler (It only returns Delegates in
my tests)

EventHandler is a delegate. The value returned by the EventHandlerLis t
indexer isn't type-safe, so it must be cast into the appropriate delegate
Type, as you can see in the code above.
>I'm certain I'm right, I can see plenty of alternative ways of doing it
that would work, I'm just confused about the information I'm getting
off the internet.
So am I being fed porkies? Do Windows Forms controls use
EventHandlerLi st objects and if so how come their implementation allows
for += and the one in ComponentModel doesn't?

An EventHandlerLis t instance is exposed as a protected property named,
"Events" from the Component class to all derived types. The "+=" operator
acts on public events, not the "Events" property. The underlying
implementation of "add" and "remove" on these public events can use the
"Events" property, internally, to store the supplied delegate.

Yes, I'm sure WinForms controls use the Events property, though I'm not sure
if ALL events on ALL WinForms Controls are registered through the inherited
Component.Event s property. Web controls use the "Events" property as well,
but it's declared in the base Control since Control doesn't derive from
Component. It still works the same way.

In my example, the public event is "ServiceViewCha nged". The "add" and
"remove" implementations of that event, which correspond to the "+=" and
"-=" operators, respectively, add or remove the supplied delegate to or from
the underlying EventHandlerLis t by calling either Events.AddHandl er or
Events.RemoveHa ndler. Both of these methods require the same key used in
the indexer when you attempt to retrieve the delegate reference from the
list.

If things still aren't clear, let me know.

--
Dave Sexton


Nov 8 '06 #3
Thanks Dave, no problem on the Generics, I use 2 at home, 1.1 at work.
There are some changes in 2 that affect Events in VB.net, so I
specified 1.1 in case those changes were part of some underlying change
in 2. I could of made that much much clearer :)

Give me a while to digest the info and I'll let you know if I have any
joy
Cheers

Dave Sexton wrote:
Hi Ian,

Sorry about using C# generics - I just realized you wrote (1.1) in the title.

To be honest, I don't have a code snippet without generics on hand anyway. If
you need me to rewrite my example without generics I will - just let me know.

--
Dave Sexton

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
Hi Ian,

<snip>
I was under the impression that
windows.forms controls used EventHandlerLis ts because generally most
events aren't consumed so this saves memory.
True.
I can add a button to a form, and have

this.button9.Cl ick += new System.EventHan dler(this.butto n9_Click);
this.button9.Cl ick += new System.EventHan dler(this.butto n9b_Click);

which will successfully fire off both functions when the button is
clicked.

The EventHandlerLis t does not fire both as it keys the delegate in a
hashtable and overwrites the first reference with the second reference
(This is using AddHandler).
Here's an example that I'm using from a project I'm working on right now in
a class that derives from Component:

private readonly object ServiceViewChan gedEvent = new object();

public event EventHandler<Se rviceViewChange dEventArgsServi ceViewChanged
{
add
{
lock (ServiceViewCha ngedEvent)
{
Events.AddHandl er(ServiceViewC hangedEvent, value);
}
}
remove
{
lock (ServiceViewCha ngedEvent)
{
Events.RemoveHa ndler(ServiceVi ewChangedEvent, value);
}
}
}

private void OnServiceViewCh anged(ServiceVi ewChangedEventA rgs e)
{
EventHandler<Se rviceViewChange dEventArgshandl er = null;

lock (ServiceViewCha ngedEvent)
{
handler = (EventHandler<S erviceViewChang edEventArgs>)
Events[ServiceViewChan gedEvent];
}

if (handler != null)
handler(this, e);
}
More confusingly in trying to get the thing running I see code all over
the internet that can't run. A VB example that used a foreach (there's
no enumerator so that can't work) and a C++ example where the
EventHandlerLis t returns an EventHandler (It only returns Delegates in
my tests)
EventHandler is a delegate. The value returned by the EventHandlerLis t
indexer isn't type-safe, so it must be cast into the appropriate delegate
Type, as you can see in the code above.
I'm certain I'm right, I can see plenty of alternative ways of doing it
that would work, I'm just confused about the information I'm getting
off the internet.
So am I being fed porkies? Do Windows Forms controls use
EventHandlerLis t objects and if so how come their implementation allows
for += and the one in ComponentModel doesn't?
An EventHandlerLis t instance is exposed as a protected property named,
"Events" from the Component class to all derived types. The "+=" operator
acts on public events, not the "Events" property. The underlying
implementation of "add" and "remove" on these public events can use the
"Events" property, internally, to store the supplied delegate.

Yes, I'm sure WinForms controls use the Events property, though I'm not sure
if ALL events on ALL WinForms Controls are registered through the inherited
Component.Event s property. Web controls use the "Events" property as well,
but it's declared in the base Control since Control doesn't derive from
Component. It still works the same way.

In my example, the public event is "ServiceViewCha nged". The "add" and
"remove" implementations of that event, which correspond to the "+=" and
"-=" operators, respectively, add or remove the supplied delegate to or from
the underlying EventHandlerLis t by calling either Events.AddHandl er or
Events.RemoveHa ndler. Both of these methods require the same key used in
the indexer when you attempt to retrieve the delegate reference from the
list.

If things still aren't clear, let me know.

--
Dave Sexton
Nov 9 '06 #4

Dave Sexton wrote:
Hi Ian,

Sorry about using C# generics - I just realized you wrote (1.1) in the title.

To be honest, I don't have a code snippet without generics on hand anyway. If
you need me to rewrite my example without generics I will - just let me know.

--
Dave Sexton

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
Hi Ian,

<snip>
I was under the impression that
windows.forms controls used EventHandlerLis ts because generally most
events aren't consumed so this saves memory.
True.
I can add a button to a form, and have

this.button9.Cl ick += new System.EventHan dler(this.butto n9_Click);
this.button9.Cl ick += new System.EventHan dler(this.butto n9b_Click);

which will successfully fire off both functions when the button is
clicked.

The EventHandlerLis t does not fire both as it keys the delegate in a
hashtable and overwrites the first reference with the second reference
(This is using AddHandler).
Here's an example that I'm using from a project I'm working on right now in
a class that derives from Component:

private readonly object ServiceViewChan gedEvent = new object();

public event EventHandler<Se rviceViewChange dEventArgsServi ceViewChanged
{
add
{
lock (ServiceViewCha ngedEvent)
{
Events.AddHandl er(ServiceViewC hangedEvent, value);
}
}
remove
{
lock (ServiceViewCha ngedEvent)
{
Events.RemoveHa ndler(ServiceVi ewChangedEvent, value);
}
}
}

private void OnServiceViewCh anged(ServiceVi ewChangedEventA rgs e)
{
EventHandler<Se rviceViewChange dEventArgshandl er = null;

lock (ServiceViewCha ngedEvent)
{
handler = (EventHandler<S erviceViewChang edEventArgs>)
Events[ServiceViewChan gedEvent];
}

if (handler != null)
handler(this, e);
}
More confusingly in trying to get the thing running I see code all over
the internet that can't run. A VB example that used a foreach (there's
no enumerator so that can't work) and a C++ example where the
EventHandlerLis t returns an EventHandler (It only returns Delegates in
my tests)
EventHandler is a delegate. The value returned by the EventHandlerLis t
indexer isn't type-safe, so it must be cast into the appropriate delegate
Type, as you can see in the code above.
I'm certain I'm right, I can see plenty of alternative ways of doing it
that would work, I'm just confused about the information I'm getting
off the internet.
So am I being fed porkies? Do Windows Forms controls use
EventHandlerLis t objects and if so how come their implementation allows
for += and the one in ComponentModel doesn't?
An EventHandlerLis t instance is exposed as a protected property named,
"Events" from the Component class to all derived types. The "+=" operator
acts on public events, not the "Events" property. The underlying
implementation of "add" and "remove" on these public events can use the
"Events" property, internally, to store the supplied delegate.

Yes, I'm sure WinForms controls use the Events property, though I'm not sure
if ALL events on ALL WinForms Controls are registered through the inherited
Component.Event s property. Web controls use the "Events" property as well,
but it's declared in the base Control since Control doesn't derive from
Component. It still works the same way.

In my example, the public event is "ServiceViewCha nged". The "add" and
"remove" implementations of that event, which correspond to the "+=" and
"-=" operators, respectively, add or remove the supplied delegate to or from
the underlying EventHandlerLis t by calling either Events.AddHandl er or
Events.RemoveHa ndler. Both of these methods require the same key used in
the indexer when you attempt to retrieve the delegate reference from the
list.

If things still aren't clear, let me know.

--
Dave Sexton
You were spot on with the assertion that I needed to cast the Delegate
to the correct type. I had previously attempted to cast it to an
EventHandler with didn't work. The reason being I was using an example
borrowed from MS which uses MouseEventHandl er (Their code only went as
far as adding the delegate to the list not firing it).

I now have it working both by deriving from Component and using Events,
and with a EventHandlerLis t declared in the class.

Thanks for your help
Ian

Nov 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5307
by: BK | last post by:
Hi, I have a class which has a lot of events (>100). For some reasons, I have to go through all invocation lists to do something. What I'm wondering is that, is there any way to use reflection to get their InvocationList without going through each event? // tedious foreach (Delegate handler in Event1.GetInvocationList()) {...} foreach (Delegate handler in Event2.GetInvocationList()) {...}
0
2838
by: Anders Borum | last post by:
Hello! This post relates to the implementation of the EventHandlerList class. When implementing custom events in a framework I'm developing, a handling of larger amounts of events in a class was needed and I turned my attention to the EventHandlerList. For instance, let's imagine I have 16 public events in my class and didn't use the EventHandlerList, the compiler would implement 16 delegates, each
2
1567
by: MS News \(MS LVP\) | last post by:
What happened to the EventsHandlerList in VB.NET. There is none? C# is okay This.Load += etc.. Me.Load ?? none in VB.NET How can you subscribe or un-subscribe to a built in event in VB.NET Thanks
3
5388
by: CJ Taylor | last post by:
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...
7
3441
by: sam.m.gardiner | last post by:
I'm working with VB.NET events and I want a way to disconnect all the handlers of an event. I want to do this in the object that is the source of the event. This is slightly tricky in VB.Net as the eventing code is slightly hidden. when you use events in Vb.Net you type this: <code> Public event MyEvent() </code>
8
11680
by: jimmarq | last post by:
I have a button click event on my main application window that opens a form. The form has a lot of controls, loads a lot of data, and uses a lot of memory. When I close the form the memory is no deallocated. Every time I open the form, 7 megabytes of RAM and 7 megabytes of virtual memory are eaten up. Here are the only lines of code in the button click event: Dim frminv As New FrmInvoice() frminv.MdiParent = Me...
15
6544
by: damiensawyer | last post by:
Hi, I am creating a class (from a base class) that needs to trigger events (code at bottom). I am instatiating the classes and wiring up the events as follows. clsDetermineConnection oDC = new clsDetermineConnection(Request); oDC.LogMessage += new RunTraceBase.TraceArguments(LogMessagesFromEvents);
4
9858
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in this context, in fact some articles from pre-2.0 suggest using any collection class of your choice. So my questions focus more on the syntax of event properties provided by the "event" keyword in C#. (Disclaimer - I am a C++ programmer working...
2
9037
by: joelkeepup | last post by:
Hi, I made a change this morning and now im getting an error that says either "a is undefined or null" or "e is undefined or null" the microsoft ajax line is below, I have no idea how to figure this problem out. Any suggestions? thanks Joel
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9562
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.