Solved

Control Event

Posted on 2004-10-22
213 Views
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??????

Thanks,
kendal
0
Question by:gvector1
    3 Comments
     
    LVL 3

    Accepted Solution

    by:
    Yes - refer to http://www.csharphelp.com/archives2/archive470.html - 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();
    ctrl.SubscribeCustomControlEvent(
          new CustomEventHandler(handler.HandleCustomControlEvent));

    ctrl.DoSomethingThatRaisesEvent();





    0
     
    LVL 3

    Expert Comment

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

    http://www.15seconds.com/issue/031023.htm
    0
     

    Author Comment

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

    Thanks,
    kendal
    0

    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 (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". 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