Click here to Skip to main content
11,928,352 members (45,639 online)
Click here to Skip to main content
Add your own
alternative version


52 bookmarked

Creating advanced C# custom events

, 19 Jan 2005
Rate this:
Please Sign up or sign in to vote.
Hooking up C# objects with delegate events.


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:

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
        static void ShowUserMessage(object a, ShipArgs e)

Now take a look into our custom event class:

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
                return message;

Finally, here it is the object:

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
                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()


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


About the Author

Erdogan Gulsoy
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMaybe Simpler ..?? Pin
stixoffire29-Apr-15 10:38
memberstixoffire29-Apr-15 10:38 
GeneralMy vote of 5 Pin
Sadique KT19-Nov-13 20:07
memberSadique KT19-Nov-13 20:07 
GeneralMy vote of 3 Pin
meorfi21-Jun-13 0:40
membermeorfi21-Jun-13 0:40 
GeneralMy vote of 5 Pin
Mandeep Singh10-Apr-13 2:29
memberMandeep Singh10-Apr-13 2:29 
GeneralThat was exactly what I needed! Pin
Niyazi Yarar11-Dec-12 5:28
memberNiyazi Yarar11-Dec-12 5:28 
SuggestionConfusing Order or Code Pin
odytrice12-Oct-12 9:41
memberodytrice12-Oct-12 9:41 
GeneralMy vote of 5 Pin
ganeshaditya31012-Jul-12 3:55
memberganeshaditya31012-Jul-12 3:55 
GeneralThanks a lot Pin
Vishal Dhobi13-Jun-12 22:28
memberVishal Dhobi13-Jun-12 22:28 
GeneralMy vote of 5 Pin
SRTABLER1-Jan-12 23:40
memberSRTABLER1-Jan-12 23:40 
GeneralMy vote of 5 Pin
llox23-Dec-11 4:30
memberllox23-Dec-11 4:30 
GeneralMy vote of 3 Pin
BrianBissell25-Jul-11 9:06
memberBrianBissell25-Jul-11 9:06 
GeneralMy vote of 4 Pin
AnirbanGhatak19-Jul-11 3:29
memberAnirbanGhatak19-Jul-11 3:29 
GeneralMy vote of 5 Pin
kamiktk1237-Jun-11 3:58
memberkamiktk1237-Jun-11 3:58 
GeneralMy vote of 5 Pin
avinashreddy53919-Apr-11 23:49
memberavinashreddy53919-Apr-11 23:49 
GeneralMy vote of 5 Pin
briju7-Mar-11 1:01
memberbriju7-Mar-11 1:01 
GeneralMy vote of 1 Pin
Douglas Day16-Dec-10 11:36
memberDouglas Day16-Dec-10 11:36 
GeneralMy vote of 5 Pin
Vijay285618-Sep-10 16:17
memberVijay285618-Sep-10 16:17 
GeneralPerfect Pin
sizam6-Aug-09 7:56
membersizam6-Aug-09 7:56 
GeneralMy vote of 1 Pin
Don R. Davidson19-Jan-09 9:01
memberDon R. Davidson19-Jan-09 9:01 
GeneralRe: My vote of 1 Pin
P3 Tech22-Jul-09 11:51
memberP3 Tech22-Jul-09 11:51 
GeneralRe: My vote of 1 Pin
BrianBissell25-Jul-11 9:08
memberBrianBissell25-Jul-11 9:08 
GeneralNice, but... Pin
gehkadl19-Jan-05 22:47
membergehkadl19-Jan-05 22:47 
GeneralRe: Nice, but... Pin
Erdogan Gulsoy20-Jan-05 8:30
memberErdogan Gulsoy20-Jan-05 8: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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 20 Jan 2005
Article Copyright 2005 by Erdogan Gulsoy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid