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

AVR
04/22/2011 - 00:38

Hi

I copied the example but I have one problem
with the Line:

if(this.OwnerChanged != null) ==> it blocks me

Since "this.OwnerChanged" is always null and
complains of "missing Object Reference"

I see that : "car.OwnerChanged += new ... "
is ignored , if not declared inside Class Car
but is in different class (like Form1.cs)

What should I do to make it work from : Form1.cs ?

AVR

reply

AVR
04/22/2011 - 01:35

To solve it : needs to declare a static event handler
"Public static event OwnerchangedEventhandler..."
and it require now to call class members by the
name of the Class ==> "Car" instead of "car"

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

Anonymous
03/02/2012 - 11:28

sender.LicencePlate = "Isting
09/29/2009 - 10:28 хуесос ты проклятый

sender.LicensePlate = "у нас была полная укатайка"

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

Lasse
03/17/2010 - 09:46

This is the best explanation on the event topic I have seen so far! Thank you!

reply

Anonymous
03/28/2010 - 21:56

Wow! Best of all!

reply

Pavan
04/06/2010 - 22:08

Nice simple and easy to understand article Thanks man..

reply

Anonymous
04/21/2010 - 14:52

wonderful article . simply explained :)

reply

wexoni
05/16/2010 - 16:09

thank you so much for this clean explanation on events. It makes sense to me now.

I have small problem.

I made 4 textboxes (make, model,year and owner) and a button.

When I change a owner, event fires up correctly, however it keeps repeating it's self.

For example, I have this code:

void car_OwnerChanged(string newOwner)
        {
            MessageBox.Show("Hello, owner changed");
           
        }

When I change the owner, message box pops. When I change it again, message box pops 2 times (must ok it).
If I do it 10 times, there will be 10 text boxes.

How do I make it pop only once?

reply

The Reddest
05/16/2010 - 22:22

Are you attaching the event more than once? Can you paste the code where the event handler is being attached?

reply

wexoni
05/17/2010 - 10:35

Thank you for fast reply :)

Here is my class code:

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace events
{
    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()
        {
        }
    }
}

And here is form code.

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace events
{

   
    public partial class Form1 : Form
    {

         
       
        public Form1()
        {
            InitializeComponent();

             
        }
        Car car = new Car();
     

        private void button1_Click(object sender, EventArgs e)
        {
                           

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

            car.CarMake = textBox1.Text;
            car.CarOwner = textBox2.Text;
            car.CarModel = textBox3.Text;
            car.CarYear = Convert.ToInt32( textBox4.Text);
           

        }

        void car_OwnerChanged(string newOwner)
        {
            MessageBox.Show(newOwner);
            //throw new NotImplementedException();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            textBox6.Text = car.CarMake;
            textBox9.Text = car.CarOwner;
            textBox8.Text = car.CarModel;
            textBox7.Text  = Convert.ToString (car.CarYear);


        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox5.Text = car.CarOwner;
        }


        void car_OwnerChanged(object sender, EventArgs e)
        {
            MessageBox.Show("Hello from EVENT");
        }

    }
}

reply

The Reddest
05/17/2010 - 12:32

What's happening is every time you click the button you attach the event again. Your handler will be called for every hook that you make. You should take this line:

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

and move it into the constructor, or somewhere else so it's only called once.

reply

wexoni
05/17/2010 - 14:20

And again thank you so much.

I would like to thank you for your time maintaining this web site. For dedicating your free time to help us, with these tutorials that are best on the web.

Every single text here, is understandable and easy to follow.
Have you thought about publishing a book?

I know that this is my first web site to check every morning.

Keep on doing great things, and as soon as I have something to publish, I will do it here :)

reply

Pawan
06/14/2010 - 23:47

Great Article!
Helped me a lot.

reply

subbu
08/12/2010 - 23:47

i learnt lot from this site thank u all..........

reply

Kiran
09/01/2010 - 04:03

Nice explanation. Keep posting such articles.

reply

Anonymous
09/26/2010 - 06:23

so... an event is just a C function pointer with microsoft naming parafernalia.

reply

Suraj
11/22/2010 - 09:49

Great article! Precisely written! Thank you!

reply

Anonymous
01/08/2011 - 07:07

I have been learning events and I am not sure why i should use events in my code, it seems events are only usefull in an object which will be reused by a third party i.e thats why microsoft have so many events in there objects because they will be used by third parties and do not know hgow those third parties want to use the object so events is a good way of giving a choice, but in code that will not be reused, i dont see any reason for using events over just a method, i.e your car example, if i know that every car will have an owner change, why put in an event over a method, if is just different types of car then why not put a method at base level, so events seem a bit off overkill if you are creating code for yourself. Is this correct.

reply

Chris F
01/19/2011 - 10:16

Finally, the lesson on custom events that I have been looking for. I have never understood WHY the delegate was used before reading this and now I get it. Thanks a million!

reply

Anonymous
02/02/2011 - 22:56

man, this has been very helpful. thank you for the clear explanation

reply

Anonymous
02/19/2011 - 11:33

thanks. very nice and to the point

reply

Anonymous
02/20/2011 - 13:21

Great example. Simple and clean.

reply

Anonymous
03/19/2011 - 12:07

Surprising how long it takes other people to explain the concept of C# events! I spent a half hour surfing before I found your example and then "got it" in minutes. Thanks!

reply

Anonymous
03/28/2011 - 16:13

Very Nice!!!

reply

xcho20
03/29/2011 - 14:07

Nice one! That's exactly what i was looking for!:)
thanks

reply

jvp
04/15/2011 - 02:13

will it be posible to explain why the event handler is static in this http://msdn.microsoft.com/en-us/library/w9y9a401(v=VS.100).aspx example ?

i tryed removing the static and it does not run, even tho neither the compiler nor the CLR complains

TIA

reply

Mohit
04/18/2011 - 09:38

Simple example to show custom event and event handling
using System;
class myarray
{
public int[] num=new int[5];
public delegate void sortHandler(int[] ab); //delegate defined
public event sortHandler sort; //event sort is defined based on delegate sortHandler
public myarray()
{
int i;
this.sort+=new sortHandler(num_sort); //sort event is added to this class
for(i=0;i

reply

Anonymous
04/30/2011 - 17:25

Your examples and presentation seen ALWAYS to be the clearest of all I check. Thank-you for your time and efforts!!

reply

Manish Sharma
05/11/2011 - 03:03

Nice but I don't knew About Delegates..........

reply

nibble
06/10/2011 - 11:57

nice and clean explanation

reply

Anonymous
06/24/2011 - 06:01

Thanks for the tutorial. Nice clear language which helped me understand it on the first read.

reply

Stephen937
08/03/2011 - 09:28

Another happy coder here. This is the only damn tutorial out there that could teach me event handling. Not even MSDN had a good explanation. THANK YOU!

reply

umesh
08/20/2011 - 03:47

This is best tutorial for custom event handling. Thanks..!!!!!!

reply

Slaven
08/29/2011 - 09:05

Thank you!

reply

Qman
09/14/2011 - 02:06

Is Safe Thread?

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

1thread 2thread current line -> q1

1Thread q1 -> change OwnerChanged = null and..
2Thread q2 excute...

2thread null Ownerchanged excute...

is safe thread?

reply

The Reddest
09/14/2011 - 13:04

You are right, that is not thread safe.

reply

Anonymous
10/10/2011 - 16:54

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";

...I have some problem in this line...

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

why it use Car ..class name instead of using our created instance car....

I dun understand.. In my opinion..It should be
car.OwnerChangedEventHandler(car_OwnerChanged);

please help in this confussion

reply

mmp
10/17/2011 - 13:11

This is a great article which I ever found.Thank you very much.

reply

Ageela
10/27/2011 - 05:13

thank you very much for this fruitful example.

reply

Kirstone
11/08/2011 - 14:34

You are like angel after some hours of nothing :)

reply

Anonymous
11/20/2011 - 21:25

thx

reply

MOHIT
12/08/2011 - 06:28

keep on the good work, GOD bless u my frnd !!

reply

Anonymous
12/15/2011 - 23:21

Thanx, This was a very clear and instructional

reply

KC
02/01/2012 - 17:31

Hey,
Custom events well explained.
Just wanted to clarify one thing.
What you have done is whenever a property is changed or set, we just want to call a method. Why cant we just do a method call directly from the property's set, without having to use events?
Is there something i'm missing here?

Never worked on a real usage of custom events. It would be great if you can list a few of custom events in real time projects.

Thanks

reply

The Reddest
02/02/2012 - 10:25

The best reason to use events is to eliminate unneeded dependencies. If we were to use a direct method call, then the Car class would have a dependency on another object - the one that defines the method.

If we wanted to use the Car class is several different projects and by several different consumers, the Car class doesn't need to know anything about them. All it has to do is fire an event - any object can then attach and be notified.

Most objects in the .NET framework are implemented this way. The Button class has a Click event. This allows the Button class to be used in a million different projects by a million different developers.

reply

holdav
03/09/2012 - 02:16

Thank you. I was reading a convoluted example in a text book, which had me scratching my head. One read through your tutorial was all that was required and no head scratching :-) Very clear. Thanks again.

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.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.