Click here to Skip to main content
Click here to Skip to main content

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

Comments and Discussions

GeneralMy vote of 5 PinmemberSadique KT19-Nov-13 20:07 
GeneralMy vote of 3 Pinmembermeorfi21-Jun-13 0:40 
GeneralMy vote of 5 PinmemberMandeep Singh10-Apr-13 2:29 
GeneralThat was exactly what I needed! PinmemberNiyazi Yarar11-Dec-12 5:28 
SuggestionConfusing Order or Code Pinmemberodytrice12-Oct-12 9:41 
GeneralMy vote of 5 Pinmemberganeshaditya31012-Jul-12 3:55 
GeneralThanks a lot PinmemberVishal Dhobi13-Jun-12 22:28 
GeneralMy vote of 5 PinmemberSRTABLER1-Jan-12 23:40 
GeneralMy vote of 5 Pinmemberllox23-Dec-11 4:30 
GeneralMy vote of 3 PinmemberBrianBissell25-Jul-11 9:06 
GeneralMy vote of 4 PinmemberAnirbanGhatak19-Jul-11 3:29 
GeneralMy vote of 5 Pinmemberkamiktk1237-Jun-11 3:58 
GeneralMy vote of 5 Pinmemberavinashreddy53919-Apr-11 23:49 
GeneralMy vote of 5 Pinmemberbriju7-Mar-11 1:01 
GeneralMy vote of 1 PinmemberDouglas Day16-Dec-10 11:36 
GeneralMy vote of 5 PinmemberVijay285618-Sep-10 16:17 
GeneralPerfect Pinmembersizam6-Aug-09 7:56 
GeneralMy vote of 1 PinmemberDon R. Davidson19-Jan-09 9:01 
GeneralRe: My vote of 1 PinmemberP3 Tech22-Jul-09 11:51 
GeneralRe: My vote of 1 PinmemberBrianBissell25-Jul-11 9:08 
GeneralNice, but... Pinmembergehkadl19-Jan-05 22:47 
GeneralRe: Nice, but... PinmemberErdogan Gulsoy20-Jan-05 8:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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.150129.1 | Last Updated 20 Jan 2005
Article Copyright 2005 by Erdogan Gulsoy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid