Click here to Skip to main content
14,328,194 members

C# event arguments library

Rate this:
3.20 (9 votes)
Please Sign up or sign in to vote.
3.20 (9 votes)
11 Mar 2008CPOL
A library containing classes for often used event arguments.


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



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;


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.


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;
        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.Inserting += new EventHandler<ValueIndexCancelEventArgs<string>, 
    myLst.Setting += new EventHandler<ValueIndexChangingEventArgs<string>, 
    myLst.Removing += new EventHandler<ValueIndexCancelEventArgs<string>, 
    myLst.Clearing += new EventHandler<CancelEventArgs, 

    myLst.Add("hello world");
    myLst.Insert(1, "hello universe");

    foreach (string itm in myLst)


static void myLst_Changing(NotifyList<string> sender, ICancelEventArgs e)

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


  • 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.


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


About the Author

Other School
Germany Germany
No Biography provided

Comments and Discussions

GeneralGreat Pin
merlin98112-Mar-08 4:45
professionalmerlin98112-Mar-08 4:45 
QuestionUsage Examples? Pin
devnet24710-Mar-08 4:22
memberdevnet24710-Mar-08 4:22 
AnswerRe: Usage Examples? Pin
elektrowolf10-Mar-08 9:20
memberelektrowolf10-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.

Posted 9 Mar 2008

Tagged as


25 bookmarked