C# Snippet Tutorial - Custom Event Handlers

Skill

C# Snippet Tutorial - Custom Event Handlers

Posted in:

Every Control in C# is full of events like MouseButtonDown and KeyDown, but what happens when you want an object to fire an event that isn't already built in? This snippet tutorial will go through all the code required to create your own events and custom event handlers.

As an example, I'm going to create a class that holds information about a car. This will include make, model, year, and owner. When information about the car is changed, this class will fire events letting anyone who is listening know that something has changed.

Below is the basic class with all the members mentioned above. I've also added properties to get and set each variable.

using System;

namespace CustomEvents
{
  public class Car
  {
    private string make;
    private string model;
    private int year;
    private string owner;

    public string CarMake
    {
      get { return this.make; }
      set { this.make = value; }
    }

    public string CarModel
    {
      get { return this.model; }
      set { this.model = value; }
    }

    public int CarYear
    {
      get { return this.year; }
      set { this.year = value; }
    }

    public string CarOwner
    {
      get { return this.owner; }
      set { this.owner = value; }
    }

    public Car()
    {
    }
  }
}

Let's say I want to know whenever the Owner property of my Car object changes. The quickest way to do this is to use simply use an event handler delegate already supplied by Microsoft - such as EventHandler.

using System;

namespace CustomEvents
{
  public class Car
  {
    public event EventHandler OwnerChanged;

    private string make;
    private string model;
    private int year;
    private string owner;

    public string CarMake
    {
      get { return this.make; }
      set { this.make = value; }
    }

    public string CarModel
    {
      get { return this.model; }
      set { this.model = value; }
    }

    public int CarYear
    {
      get { return this.year; }
      set { this.year = value; }
    }

    public string CarOwner
    {
      get { return this.owner; }
      set
      {
        this.owner = value;
        if (this.OwnerChanged != null)
          this.OwnerChanged(this, new EventArgs());
      }
    }

    public Car()
    {
    }
  }
}

Here I created an event which uses the EventHandler delegate and called it OwnerChanged. I fire the event in the CarOwner property whenever it is set. When firing the event, you should always check to make sure it's not null. The event will be null if no one is listening for it - you can't fire an event to no one. Once you determined the event is not null, you simply execute it like any other method. The EventHandler signature requires an object and EventArgs as parameters, so I pass in this and a new instance of the EventArgs class. Now let's see how use this event.

Car car = new Car();

//adds an event handler to the OwnerChanged event
car.OwnerChanged += new EventHandler(car_OwnerChanged);

//setting this will fire the OwnerChanged event
car.CarOwner = "The Reddest";

This code simply creates a car object, attaches to the OwnerChanged event, and sets the CarOwner property. As you can see, using custom events is exactly the same as using built in events. In the code above, whenever the CarOwner property is set, the function car_OwnerChanged will be called.

void car_OwnerChanged(object sender, EventArgs e)
{
    //the CarOwner property has been modified
}

Now let's say I want the new owner passed in through the event. I could do this with the current event handler by passing the car's owner as sender, but that would mean I have to cast it whenever the event is fired. I also have this EventArgs parameter that I'm not using anywhere.

public string CarOwner
{
  get { return this.owner; }
  set
  {
    this.owner = value;
    if (this.OwnerChanged != null)
      this.OwnerChanged(value, new EventArgs());
  }
}

Here's a modified version of the CarOwner property that passes the new name through the event. The problem here is that value is passed through as an object, so on the receiving end, they would have to cast it back to a string to get the car's new owner. What we want is a new event handler delegate that doesn't need those parameters. To do this we'll first have to declare a new delegate matching the method signature we want and then create an event using the new delegate.

using System;

namespace CustomEvents
{
  public class Car
  {
    public delegate void OwnerChangedEventHandler(string newOwner);
    public event OwnerChangedEventHandler OwnerChanged;

    private string make;
    private string model;
    private int year;
    private string owner;

    public string CarMake
    {
      get { return this.make; }
      set { this.make = value; }
    }

    public string CarModel
    {
      get { return this.model; }
      set { this.model = value; }
    }

    public int CarYear
    {
      get { return this.year; }
      set { this.year = value; }
    }

    public string CarOwner
    {
      get { return this.owner; }
      set
      {
        this.owner = value;
        if (this.OwnerChanged != null)
          this.OwnerChanged(value);
      }
    }

    public Car()
    {
    }
  }
}

Here we create a delegate that takes a string as an argument. We then create an event handler using our new delegate. Lastly I modified the CarOwner property to use our new event handler. The code required to listen for our new event handler is identical to before except for the new delegate.

Car car = new Car();

//adds an event handler to the OwnerChanged event
car.OwnerChanged += new OwnerChangedEventHandler(car_OwnerChanged);

//setting this will fire the OwnerChanged event
car.CarOwner = "The Reddest";

Now whenever car_OwnerChanged is called, the car's owner is passed in as a string. That's all the code required to create and use your own custom events and event handlers. Leave any questions in the comment section.

Adam
08/21/2007 - 21:05

Very nice and simple, good stuff

reply

Adam
08/21/2007 - 21:20

This is a nice example of how to generate an event, but I'd like to find a nice example of how the event can be useful.

reply

Niels R.
09/20/2007 - 01:29

Thanks man! This is exactly the thing I was looking for...

reply

Thomas
10/04/2007 - 07:32

Very nice, a simple code with works

reply

CloudyOne
10/22/2007 - 19:10

Very nice, this is just what i needed to get around the limitations of multi-threading!

reply

Maneesh
11/05/2007 - 23:29

good tutorial and example on how to create new event.

reply

Mahindra
11/21/2007 - 07:43

Really very nice and simple.My confusion abt events and delegates cleared now.Good Service. Thank you very much

reply

Abdirasm
12/08/2007 - 01:23

Extremely useful. Thanks a lot

reply

Yardgnome
12/19/2007 - 13:33

Very clear and useful. Cleared up events for me perfectly.

reply

Act1v8
01/04/2008 - 16:21

Gosh! I've been looking for something simple out there that can make me understand the creation of these events.

Great tutorial! You're a life saver!

reply

Sinan
01/23/2008 - 23:31

What I was looking for exactly. Simple and useful. Thank you.

reply

C#_Convert
03/13/2008 - 13:38

Thanks for a VERY clear and VERY helpful tutorial. Awesome! I have saved this in my bookmarks of very helpful webpages.

reply

Mohammed
04/15/2008 - 07:06

Finally found what i have bbeen looking for , for ages. never found anything as simple and straighforward as this - so beautifully explaining events and basic delagates.

reply

Steve
06/05/2008 - 15:04

Thanks for taking the time to write this up. Nice job on showing how the code works by modifying the same code over and over till you get to the end. This topic is too complex to show one big snippet of code and try to explain it all. The car idea made for a good tutorial as well. Better than the New News example I was trying to follow.

reply

Pascal
06/11/2008 - 02:43

I confirm, such explanation is rare. You don't go directly to the final solution but explain the reason of each element separately step-by-step.

Great Job.
thanks

reply

Padam
07/23/2008 - 04:32

nice

reply

Alfes
07/30/2008 - 05:25

excellent mate. very good for new learner.

reply

Mike
07/31/2008 - 23:15

Thank you, I appreciate the simplicity!

reply

Reine
09/16/2008 - 01:22

Excellent, just what I needed. Thanks!

reply

Csharp
09/17/2008 - 03:13

Thanks. It's worth to say that your effort was so great. Thanks a lot. Looking forward your new posts.

reply

Mikael Henriksson
09/17/2008 - 07:50

I still have a bit of problem understanding this fully. I get what you are telling but let's say I want to set one of the other fields when the OwnerChanged event is fired. Maybe I need to set a different licenseplate when the owner has changed.

How would I do that?

reply

The Reddest
09/17/2008 - 14:38

You'd probably want to add the sender to the custom event handler in that case.

public delegate void OwnerChangedEventHandler(
    Car sender, string newOwner);
public event OwnerChangedEventHandler OwnerChanged;

Then change the desired property on the event:

void car_OwnerChanged(Car sender, string newOwner)
{
  sender.LicensePlate = "something";
}

reply

Hugo M.
09/30/2008 - 22:27

Excellent!!!!
Thank you.

reply

Iain Collins
10/22/2008 - 03:10

Thanks, this was extremely clear and well presented.

reply

John
10/23/2008 - 11:50

what am I doing wrong I get this error (I copied and paste as it was in the article)

Error 1 The type or namespace name 'OwnerChangedEventHandler' could not be found (are you missing a using directive or an assembly reference?)

please help

reply

John
10/23/2008 - 12:12

it is fixed now I got to change the code like this ( need it to add "Car." as I was calling it from another class

car.OwnerChanged += new Car.OwnerChangedEventHandler(car_OwnerChanged);

thank you for this clean explanation, I been looking for days for a simple explanation that I could understand, afer following some 5 tutorials, this is the one that did.

reply

Tim
11/18/2008 - 13:16

This is the best explanation of custom event handlers I've ever seen. Thanks

reply

Nanda
11/20/2008 - 01:11

Thanks. This is a nice explanation that helped me to understand about events.

Good work!

reply

Pavan
12/01/2008 - 11:48

Beautiful example... thanks a lot..

reply

RossiRSA
12/03/2008 - 03:55

Your tutorials are so easy to understand. Thanks

reply

Martijn Dwars
01/04/2009 - 08:49

I must say this is the best tutorial on event handling I’ve seen, great!

reply

Emil Haukeland
01/20/2009 - 18:11

I just love your tutorials. Very clear and concise :)
Thanks again!

reply

anonymous
06/09/2009 - 17:12

I have existing code that follows your tutorial. The issue I have is that the event has mutiple handlers and only one is being fired. I have stepped through the code and I add about 30 odd handlers for this event. But only one is fired.

Can you give me some ideas to look for in my code.

reply

Ranjit Kumar
06/30/2009 - 16:32

I am wary of events in .net, as you have rightly stated the complexities involved, in another of your article on events. I am afraid of using them, hence forget all about them, and everytime I come across a reading on delegate and events - I sit up with curiosity - as though I am reading for the first time:) To make things worse, they differ so much across C# and vb.net. Nice article, thanks!

reply

Anonymous
07/20/2009 - 18:31

This bit of code here:

        if (this.OwnerChanged != null)
          this.OwnerChanged(value);

Looks like it's making sure that the .OwnerChanged event is being "listened to" or wired-up. Correct?

reply

The Reddest
07/21/2009 - 09:23

Yes. If anything is attached to the event, it won't be null. If the event is raised without anything attached, you'll receive a null reference exception. Checking for null is much more efficient than putting the event raise inside a try-catch block.

reply

PJ
08/03/2009 - 16:19

Many thanks the real-world example explains the concept brilliantly.

reply

Anonymous
08/27/2009 - 20:14

Very very clear explanation. Thank you :)

reply

Isting
09/29/2009 - 10:28

хуесос ты проклятый

reply

Isting
09/29/2009 - 10:28

Hi!

I've concatenated your tutorials' examples about serialization and this one to make datagrid viewable class that can be serialized. But there's a problem. I don't know how to perform it mostly because of  public event PropertyChangedEventHandler PropertyChanged; field. And there's one more question. When DataGrid is edited, we have our BindingList updated, right? But can I add some other functions that are need to be performed when something's changed in BindingList? Thank you very much in advance.

reply

Isting
09/29/2009 - 10:40

Actually,

private void openToolStripMenuItem1_Click(object sender, EventArgs e)
{
  ObjectToSerialize ots = new ObjectToSerialize();
  Serializer sr = new Serializer();
  ots = sr.DeSerializeObject("a.regress");
  precedents = ots.Precedents;
 
}

I debugged this code and I can say that BindingList (precedents) changes okay, but datagrid still shows things that was before this method was called.

reply

Anonymous
01/19/2010 - 01:28

Good Article...Thanks

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.