The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Manage many events easily with EventHandlerList

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Roy Osherove

Posts: 1807
Nickname: royo
Registered: Sep, 2003

Roy Osherove is a .Net consultant based in Israel
Manage many events easily with EventHandlerList Posted: Dec 31, 2004 8:04 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Roy Osherove.
Original Post: Manage many events easily with EventHandlerList
Feed Title: ISerializable
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/rosherove/Rss.aspx
Feed Description: Roy Osherove's persistent thoughts
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Roy Osherove
Latest Posts From ISerializable

Advertisement

There were many little snippets and corners during Juval's class that simply made my day every time. This is one of those.

What happens when you have a class that exposes many events (such as the "Button" or "Control" class that exposes "Click","Focus" etc..)? In the standard case you would have to have many delegates as members inside it and that would make your code look very messy. The Simpler soution however would be to use the EventHandlerList class that is found in the System.ComponentModel namespace. It's basically a hashtable of all the event handlers you want to keep (the delegates, actually) and allows you to use it for general event storing purposes. In fact, the Control class in the .Net framework use this internally to store its own events.

How do you use it? You implement the specific "add" and "remove" accessors for your events, and inside them, you redirect the added or removed delegate value into the EventHandlerList object. Like so:

private EventHandlerList m_handlers = new EventHandlerList();

           

public event EventHandler Saved

 {

   add

   {

    m_handlers.AddHandler("Saved",value);

   }

   remove

   {

    m_handlers.RemoveHandler("Saved",value);

   }

 }

 

 

 

 

Unfortunately, this cannot be done in VB.Net (you can't override event accessors in VB.Net!?).

Here's an implementation of a class that exposes 3 events, one of them is not a simple event handler type. You'll see that it's just as simple to exposes them, but only one member holds all the events.

 

      public class MyComplicatedClass

      {

            public delegate void SpecialEventDelegate(string someText,int someNumber);

 

            private EventHandlerList m_handlers = new EventHandlerList();

           

            public event EventHandler Saved

            {

                  add

                  {

                        m_handlers.AddHandler("Saved",value);

                  }

                  remove

                  {

                        m_handlers.RemoveHandler("Saved",value);

                  }

            }

           

            public event EventHandler Deleted

            {

                  add

                  {

                        m_handlers.AddHandler("Deleted",value);

                  }

                  remove

                  {

                        m_handlers.RemoveHandler("Deleted",value);

                  }

            }

            public event SpecialEventDelegate SpecialEvent

            {

                  add

                  {

                        m_handlers.AddHandler("SpecialEvent",value);

                  }

                  remove

                  {

                        m_handlers.RemoveHandler("SpecialEvent",value);

                  }

            }

           

            private void FireSavedEvent()

            {

                  EventHandler handler = (EventHandler)m_handlers["Saved"];

                  if(handler!=null)

                  {

                        handler(this,new EventArgs());

                  }

            }

           

            private void FireDeletedEvent()

            {

                  EventHandler handler = (EventHandler)m_handlers["Deleted"];

                  if(handler!=null)

                  {

                        handler(this,new EventArgs());

                  }

            }

           

            private void FireSpecialEvent()

            {

                  SpecialEventDelegate handler = (SpecialEventDelegate) m_handlers["SpecialEvent"];

                  int someNumber=3;

                  if(handler!=null)

                  {

                        handler("some text",someNumber);

                  }

            }

      }

 

 

Read: Manage many events easily with EventHandlerList

Topic: Visual Studio Team System Dec. CTP (Beta 2) IDE Finally Installed and Working Previous Topic   Next Topic Topic: Tsunami Relief

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use