• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

Control Event

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
gvector1
Asked:
gvector1
  • 2
1 Solution
 
aacoolCommented:
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
 
aacoolCommented:
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
 
gvector1Author Commented:
Thanks for the references.  I think that might do the trick, I will find out later.  The points are yours.

Thanks,
kendal
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now