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

Creating advanced C# custom events

Rate me:
Please Sign up or sign in to vote.
3.42/5 (56 votes)
19 Jan 2005 219.8K   2.4K   60   24
Hooking up C# objects with delegate events.

Introduction

Hooking Custom Events and its arguments to an Object.

In this article, I will try to illustrate how to hook a custom event to an object. We will go a little bit advanced and also create our own event arguments that are derived from the EventArgs base class.

As you will see throughout the code, there is an item object which refers to an inventory item. Our object will raise an event when it has a valid shipment tracking number.

Let's take a look the main program that uses our item object:

C#
using System;
namespace EventExample
{
    class Class1
    {
        static void Main(string[] args)
        {
            // we will create our instance
            Shipment myItem = new Shipment();

            // we need to add the delegate event to new object
            myItem.OnShipmentMade += 
                   new Shipment.ShipmentHandler(ShowUserMessage);

            // we assumed that the item has been just shipped and 
            // we are assigning a tracking number to it.
            myItem.TrackingNumber = "1ZY444567";

            // The common procedure to see what is going on the 
            // console screen
            Console.Read();
        }
       
        static void ShowUserMessage(object a, ShipArgs e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

Now take a look into our custom event class:

C#
using System;
namespace EventExample
{
    public class ShipArgs : EventArgs
    {
        private string message;

        public ShipArgs(string message)
        {
            this.message = message;
        }

        // This is a straightforward implementation for 
        // declaring a public field
        public string Message
        {
            get
            {
                return message;
            }
        }
    }
}

Finally, here it is the object:

C#
using System;
namespace EventExample
{
    public class Shipment
    {
        private string trackingnumber;

        // The delegate procedure we are assigning to our object
        public delegate void ShipmentHandler(object myObject, 
                                             ShipArgs myArgs);

        public event ShipmentHandler OnShipmentMade;

        public string TrackingNumber
        {
            set
            {
                trackingnumber = value;

                // We need to check whether a tracking number 
                // was assigned to the field.
                if (trackingnumber.Length != 0)
                {
                    ShipArgs myArgs = new ShipArgs("Item has been shipped.");

                    // Tracking number is available, raise the event.
                    OnShipmentMade(this, myArgs);
                }
            }
        }

        public Shipment()
        {
        }
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
cpsglauco16-May-16 5:56
cpsglauco16-May-16 5:56 
GeneralMaybe Simpler ..?? Pin
stixoffire29-Apr-15 9:38
stixoffire29-Apr-15 9:38 
GeneralMy vote of 5 Pin
Sadique KT19-Nov-13 19:07
Sadique KT19-Nov-13 19:07 
GeneralMy vote of 3 Pin
meorfi20-Jun-13 23:40
meorfi20-Jun-13 23:40 
GeneralMy vote of 5 Pin
Mandeep Singh10-Apr-13 1:29
Mandeep Singh10-Apr-13 1:29 
GeneralThat was exactly what I needed! Pin
Niyazi Yarar11-Dec-12 4:28
Niyazi Yarar11-Dec-12 4:28 
SuggestionConfusing Order or Code Pin
odytrice12-Oct-12 8:41
odytrice12-Oct-12 8:41 
GeneralMy vote of 5 Pin
ganeshaditya31012-Jul-12 2:55
ganeshaditya31012-Jul-12 2:55 
GeneralThanks a lot Pin
VishalSRajput13-Jun-12 21:28
VishalSRajput13-Jun-12 21:28 
GeneralMy vote of 5 Pin
SRTABLER1-Jan-12 22:40
SRTABLER1-Jan-12 22:40 
GeneralMy vote of 5 Pin
llox23-Dec-11 3:30
llox23-Dec-11 3:30 
GeneralMy vote of 3 Pin
BrianBissell25-Jul-11 8:06
BrianBissell25-Jul-11 8:06 
GeneralMy vote of 4 Pin
AnirbanGhatak19-Jul-11 2:29
AnirbanGhatak19-Jul-11 2:29 
GeneralMy vote of 5 Pin
kamiktk1237-Jun-11 2:58
kamiktk1237-Jun-11 2:58 
GeneralMy vote of 5 Pin
avinashreddy53919-Apr-11 22:49
avinashreddy53919-Apr-11 22:49 
GeneralMy vote of 5 Pin
briju7-Mar-11 0:01
briju7-Mar-11 0:01 
GeneralMy vote of 1 Pin
Douglas Day16-Dec-10 10:36
Douglas Day16-Dec-10 10:36 
Non-descriptive, just bad all around.
GeneralMy vote of 5 Pin
Vijay285618-Sep-10 15:17
Vijay285618-Sep-10 15:17 
GeneralPerfect Pin
sizam6-Aug-09 6:56
sizam6-Aug-09 6:56 
GeneralMy vote of 1 Pin
Don R. Davidson19-Jan-09 8:01
Don R. Davidson19-Jan-09 8:01 
GeneralRe: My vote of 1 Pin
P3 Tech22-Jul-09 10:51
P3 Tech22-Jul-09 10:51 
GeneralRe: My vote of 1 Pin
BrianBissell25-Jul-11 8:08
BrianBissell25-Jul-11 8:08 
GeneralNice, but... Pin
gehkadl19-Jan-05 21:47
gehkadl19-Jan-05 21:47 
GeneralRe: Nice, but... Pin
Erdogan Gulsoy20-Jan-05 7:30
Erdogan Gulsoy20-Jan-05 7:30 

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.