Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Control Event

Posted on 2004-10-22
3
Medium Priority
?
217 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
Comment
Question by:gvector1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
aacool earned 2000 total points
ID: 12382704
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
ID: 12383196
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
ID: 12444585
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

596 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