Click here to Skip to main content
12,951,606 members (61,710 online)
Click here to Skip to main content
Add your own
alternative version


54 bookmarked
Posted 19 Jan 2005

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

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

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