Control Event

Posted on 2004-10-22
Last Modified: 2010-04-15
I am trying to create my own control and was wondering about events.  Is there anyway to create custom events for my control.  Not the event handlers, but the events themselves.  Is there anyway possible to do that??????

Question by:gvector1
    LVL 3

    Accepted Solution

    Yes - refer to - Creating custom delegates and events with C#

    First, create a class for your custom event args derived from EventArgs
    Then,   Create a custom event for your class/control
    It has the form public event delegateName eventName

          public event CustomEventHandler CustomControlEvent;

          // This method adds an event handler (delegate) to the event invocation list.
          public void SubscribeCustomControlEvent(CustomEventHandler eventHandler)
                CustomControlEvent += eventHandler;
    To raise the event,
          // This method raises the event which causes all the event handlers in the event
          // invocation list to be executed in the order they were added.
          private void DispatchCustomControlEvent()
                // make sure the are some delegates to call
                // all delegates will be called in the order they were added
                // to the invocation list
                if (CustomControlEvent != null)
                      CustomControlEvent(new CustomEventArgs(args);
    Finally, create a class to handle the events.  
    public class CustomEventHandlerClass
          // This method will be an event handler.  It can be named anything you want,
          // but it must have the same signature as the delegate
          // AccountBalanceEventHandler declared above.
          public void HandleCustomControlEvent(CustomEventArgs e)
                // do something useful here

    In your code that uses your control,

    CustomEventHandlerClass handler = new CustomEventHandlerClass();
          new CustomEventHandler(handler.HandleCustomControlEvent));


    LVL 3

    Expert Comment

    Here is a good example of creating a custom ASP.NET control that has a custom event

    Author Comment

    Thanks for the references.  I think that might do the trick, I will find out later.  The points are yours.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    846 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

    8 Experts available now in Live!

    Get 1:1 Help Now