Click here to Skip to main content
Click here to Skip to main content
Go to top

C# event arguments library

, 11 Mar 2008
Rate this:
Please Sign up or sign in to vote.
A library containing classes for often used event arguments.

Introduction

There are many kinds of event arguments you often need, e.g., CancelEventArgs. This library contains a collection of event arguments.

Background

Interfaces

I've put the different abilities of event arguments into the following interfaces:

interface IValueEventArgs<T> { T Value { get; } }
interface ICancelEventArgs { bool IsCanceled { get; } void Cancel(); }
interface INewValueEventArgs<T> { T NewValue { get; set; } }
interface IOldValueEventArgs<T> { T OldValue { get; } }
interface IIndexEventArgs { int Index { get; } }
interface IRangeEventArgs : IIndexEventArgs { int Count { get; } }

These interfaces allow you to easily handle multiple events in one handler function.

The ICancelEventArgs contains two members (bool IsCanceled { get; }, void Cancel();) to prevent event handlers from setting the Canceled property to false by using code like this:

e.Canceled = !condition;

Classes

Then, I implemented some combinations of these interfaces in classes:

class ValueEventArgs<T> : IValueEventArgs<T>
class CancelEventArgs : ICancelEventArgs
class ValueCancelEventArgs<T> : IValueEventArgs<T>, ICancelEventArgs

class ValueChangingEventArgs<T> : IValueEventArgs<T>,  INewValueEventArgs<T>
class ValueChangedEventArgs<T> : IValueEventArgs<T>,  IOldValueEventArgs<T>

class ValueIndexEventArgs<T> : IValueEventArgs<T>,  IIndexEventArgs
class ValueIndexChangingEventArgs<T> : IValueEventArgs<T>, 
      INewValueEventArgs<T>, IIndexEventArgs
class ValueIndexChangedEventArgs<T> : IValueEventArgs<T>, 
      IOldValueEventArgs<T>, IIndexEventArgs 

class RangeEventArgs : IRangeEventArgs
class RangeCancelEventArgs : IRangeEventArgs, ICancelEventArgs

In practice, these classes are derived from each other to write less code.

Delegates

The library contains the following event handler delegate:

public delegate void EventHandler<TEventArgs, TSender>(TEventArgs e, TSender sender)
       where TEventArgs : EventArgs;

This handler allows you to specify the sender's type with the second generic parameter.

Using the code

As an example, I have developed a notify list that provides the following events:

public event EventHandler<ValueIndexCancelEventArgs<T>, NotifyList<T>> Inserting;
public event EventHandler<ValueIndexEventArgs<T>, NotifyList<T>> Inserted;
public event EventHandler<ValueIndexChangingEventArgs<T>, NotifyList<T>> Setting;
public event EventHandler<ValueIndexChangedEventArgs<T>, NotifyList<T>> Set;
public event EventHandler<ValueIndexCancelEventArgs<T>, NotifyList<T>> Removing;
public event EventHandler<ValueIndexEventArgs<T>, NotifyList<T>> Removed;
public event EventHandler<CancelEventArgs, NotifyList<T>> Clearing;
public event EventHandler<EventArgs, NotifyList<T>> Cleared; 

I implemented IList<T>, ICollection<T>, and IEnumerable<T> to provide basic list functionality. Internally, a List<T> is used.

Event raising

As an example for event raising, I'll show you the Insert function:

public void Insert(int index, T item)
{
    if (this.OnInserting(item, index))
    {
        this.list.Insert(index, item);
        this.OnInserted(item, index);
    }
}

protected bool OnInserting(T item, int index)
{
    if (this.Inserting != null)
    {
        ValueIndexCancelEventArgs<T> evArgs = 
           new ValueIndexCancelEventArgs<T>(item, index);
        this.Inserting(this, evArgs);
        return !evArgs.IsCanceled;
    }
    else
        return true;
}

protected void OnInserted(T item, int index)
{
    if (this.Inserted != null)
    {
        ValueIndexEventArgs<T> eventArgs = 
           new ValueIndexEventArgs<T>(item, index);
        this.Inserted(this, eventArgs);
    }
}

As you can see, the event raising is encapsulated in a protected function that instantiates the event argument class, and returns true if the action has not been canceled by an event handler.

Event handling

As an event handling example, I'll show you how to prevent the mentioned NotifyList from changing in any way:

static void Main(string[] args)
{
    NotifyList<string> myLst = new NotifyList<string>();

    myLst.Add("test");
    myLst.Add("qwert");

    myLst.Inserting += new EventHandler<ValueIndexCancelEventArgs<string>, 
                       NotifyList<string>>(myLst_Changing);
    myLst.Setting += new EventHandler<ValueIndexChangingEventArgs<string>, 
                     NotifyList<string>>(myLst_Changing);
    myLst.Removing += new EventHandler<ValueIndexCancelEventArgs<string>, 
                      NotifyList<string>>(myLst_Changing);
    myLst.Clearing += new EventHandler<CancelEventArgs, 
                      NotifyList<string>>(myLst_Changing);

    myLst.Clear();
    myLst.Add("hello world");
    myLst.Add("test123");
    myLst.RemoveAt(0);
    myLst.Insert(1, "hello universe");

    foreach (string itm in myLst)
        Console.WriteLine(itm);
    //Output:
    //test
    //qwert

    Console.ReadLine();
}

static void myLst_Changing(NotifyList<string> sender, ICancelEventArgs e)
{
    e.Cancel();
}

As you can see, there is only one handler for all the events. This is possible because of the interfaces provided by the library.

History

  • Sunday March 9th: Article created.
  • Monday March 10th: Added example; wrong implementation in the ValueIndexCancelEventArgs<T> class corrected.
  • Wednesday March 12th: Added the IRangeEventArgs interface.

License

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

Share

About the Author

elektrowolf
Other School
Germany Germany
No Biography provided

Comments and Discussions

 
GeneralGreat Pinmembermerlin98112-Mar-08 4:45 
QuestionUsage Examples? Pinmemberdevnet24710-Mar-08 4:22 
AnswerRe: Usage Examples? Pinmemberelektrowolf10-Mar-08 9:20 

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 | Mobile
Web01 | 2.8.140916.1 | Last Updated 11 Mar 2008
Article Copyright 2008 by elektrowolf
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid