Instantiating Custom events in c# csharp .net

Posted on 2008-11-17
Last Modified: 2013-12-17
So I have created an inherited object for which I have defined a custom Even and matching delegate. I also defined the custom EventArgs. Finally, I have tied the objects event with a function that will execute when the event is called.

The problem I am having is instantiating the custom event: I want the event to occur outside the object, and the object to react to it.

The reason is this: I will have several objects observing the event, and I want them all to react.

Thank you so much for reading this!

// Inherited Object


namespace NA_minu


    using System.Text;

    using System.Windows.Forms;

    using System.Drawing;

    // Delegate of Event

    public delegate void GridColorChangedEventHandler(

        object sender, GridColorChangeEventArgs e);

    class ModifiedDataGridView: DataGridView


        // Public event

        public event GridColorChangedEventHandler ColorChangedEvent;

        public ModifiedDataGridView()


            // Empty constructor




    // Custom EventArgs

    class GridColorChangeEventArgs : EventArgs


        private Color _gridBack;

        private Color _gridFore;

        private Color _gridSelectedFore;

        private Color _gridSelectedBack;

        public GridColorChangeEventArgs(

            Color gridBack,

            Color gridFore,

            Color gridSelectedFore,

            Color gridSelectedBack)


            this._gridBack = gridBack;

            this._gridFore = gridFore;

            this._gridSelectedFore = gridSelectedFore;

            this._gridSelectedBack = gridSelectedBack;


        public Color GridBack


            get { return _gridBack; }


        public Color GridFore


            get { return _gridFore; }


        public Color GridSelectedFore


            get { return _gridSelectedFore; }


        public Color GridSelectedBack


            get { return _gridSelectedBack; }




// In the form, I attach a method to the custom event

dgvFile.ColorChangedEvent += 

                new GridColorChangedEventHandler(dgvFile_ColorChangedEvent);

// The attached method

void dgvFile_ColorChangedEvent(object sender, GridColorChangeEventArgs e)


    // Here I will want to manipulate the object.


// This button click should fire the event...

private void btnClose_Click(object sender, EventArgs e)



    //*** What event do I call?

    //*** When I type below the event GridColorChange, it isn't listed

    //*** in the intelli-type thing that comes up.

    if( /* ??? what do I type here??? */ != null)


        /* ??? what do I type here??? */(new GridColorChangeEventArgs(







Open in new window

Question by:yaronusa
    LVL 3

    Accepted Solution

    dgvFile_ColorChangedEvent(null, new GridColorChangeEventArgs(

    will run the code that happens when the event fires.

    If you want to actually cause the event to fire when the button is clicked you need to create a new method on the dgvFile object that causes the event to fire and run that method from your buttonclick.

    Author Comment

    OK, thank you for your time. I seem to have gotten something backwards in my brain but you straightened it out for me.

    Thank you!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now