Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#
Article

C# event arguments library

Rate me:
Please Sign up or sign in to vote.
3.20/5 (9 votes)
11 Mar 2008CPOL1 min read 39.5K   263   25   3
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:

C#
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:

C#
e.Canceled = !condition;

Classes

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

C#
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:

C#
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:

C#
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:

C#
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:

C#
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)


Written By
Other School
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGreat Pin
merlin98112-Mar-08 4:45
professionalmerlin98112-Mar-08 4:45 
QuestionUsage Examples? Pin
devnet24710-Mar-08 4:22
devnet24710-Mar-08 4:22 
AnswerRe: Usage Examples? Pin
elektrowolf10-Mar-08 9:20
elektrowolf10-Mar-08 9:20 

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

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