Click here to Skip to main content
Click here to Skip to main content

Creating a Custom Event Dictionary

, 3 Jan 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
An article on creating a custom event dictionary.


This collection allows the developer to call any number of events from a centralized source.

Using the code

Simply instantiate the EventDictionary class as you would a regular Dictionary class, and pass in an EventHandler using the EventDictionary.Add method.

using System;
using System.Collections.Generic;

/// <summary>
/// Represents a collection of key/event pairs that are accessible by the 
/// event name.
/// </summary>
public class EventDictionary : Dictionary<string, EventHandler>

    /// <summary>
    /// Adds an entry with the specified value into the EventDictionary 
    /// collection with the EventHandler's name as the key. 
    /// </summary>
    /// <param name="e">The value of the event to add. This value cannot 
    /// be null (Nothing in Visual Basic).</param>
    public void Add(EventHandler e)
        if (e == null)
            throw new ArgumentException("Event Handler cannot be null.", 
        string s = e.Target.GetType().GetEvents()[0].ToString();
        base.Add(s.Substring(s.LastIndexOf(" ") + 1), e);


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


About the Author

Daniel Dhillon
Software Developer (Senior) DoD
United States United States
Currently working for the Department of Defense at the AFN Broadcasting Center in California.
Follow on   LinkedIn

Comments and Discussions

GeneralAn EventPool is a bit more comprehensive. PinprotectorMarc Clifton4-Jan-07 2:25 
GeneralRe: An EventPool is a bit more comprehensive. Pinmemberleppie4-Jan-07 4:01 
GeneralRe: An EventPool is a bit more comprehensive. PinprotectorMarc Clifton4-Jan-07 4:15 
leppie wrote:
framework provides System.ComponentModel.EventHandlerList!

D'Oh! | :doh: Of course!

Thyme In The Country

People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150302.1 | Last Updated 3 Jan 2007
Article Copyright 2007 by Daniel Dhillon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid