1 out of 1 rated this helpful - Rate this topic

How to: Handle Multiple Events Using Event Properties

To use event properties (custom events in Visual Basic 2005), you define the event properties in the class that raises the events, and then set the delegates for the event properties in classes that handle the events. To implement multiple event properties in a class, the class must internally store and maintain the delegate defined for each event. A typical approach is to implement a delegate collection that is indexed by an event key.

To store the delegates for each event, you can use the EventHandlerList class, or implement your own collection. The collection class must provide methods for setting, accessing, and retrieving the event handler delegate based on the event key. For example, you could use a Hashtable class, or derive a custom class from the DictionaryBase class. The implementation details of the delegate collection do not need to be exposed outside your class.

Each event property within the class defines an add accessor method and a remove accessor method. The add accessor for an event property adds the input delegate instance to the delegate collection. The remove accessor for an event property removes the input delegate instance from the delegate collection. The event property accessors use the predefined key for the event property to add and remove instances from the delegate collection.

To handle multiple events using event properties

  1. Define a delegate collection within the class that raises the events.

  2. Define a key for each event.

  3. Define the event properties in the class that raises the events.

  4. Use the delegate collection to implement the add and remove accessor methods for the event properties.

  5. Use the public event properties to add and remove event handler delegates in the classes that handle the events.

The following C# example implements the event properties MouseDown and MouseUp, using an EventHandlerList to store each event's delegate. The keywords of the event property constructs are in bold type.

Note Note

Event properties are not supported in Visual Basic 2005.


// The class SampleControl defines two event properties, MouseUp and MouseDown.
class SampleControl : Component
{
    // :
    // Define other control methods and properties.
    // :

    // Define the delegate collection.
    protected EventHandlerList listEventDelegates = new EventHandlerList();

    // Define a unique key for each event.
    static readonly object mouseDownEventKey = new object();
    static readonly object mouseUpEventKey = new object();

    // Define the MouseDown event property.
    public event MouseEventHandler MouseDown
    {
        // Add the input delegate to the collection.
        add
        {
            listEventDelegates.AddHandler(mouseDownEventKey, value);
        }
        // Remove the input delegate from the collection.
        remove
        {
            listEventDelegates.RemoveHandler(mouseDownEventKey, value);
        }
    }

    // Raise the event with the delegate specified by mouseDownEventKey
    private void OnMouseDown(MouseEventArgs e)
    {
        MouseEventHandler mouseEventDelegate =
            (MouseEventHandler)listEventDelegates[mouseDownEventKey];
        mouseEventDelegate(this, e);
    }

    // Define the MouseUp event property.
    public event MouseEventHandler MouseUp
    {
        // Add the input delegate to the collection.
        add
        {
            listEventDelegates.AddHandler(mouseUpEventKey, value);
        }
        // Remove the input delegate from the collection.
        remove
        {
            listEventDelegates.RemoveHandler(mouseUpEventKey, value);
        }
    }

    // Raise the event with the delegate specified by mouseUpEventKey
    private void OnMouseUp(MouseEventArgs e)
    {
        MouseEventHandler mouseEventDelegate =
            (MouseEventHandler)listEventDelegates[mouseUpEventKey];
        mouseEventDelegate(this, e);
    }
}


Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Alternate OnMouseDown/OnMouseUp method
Following is an alternative method which can replace the traditional implementation of the Onxxxx methods used internally to invoke the associated event using late binding.

protected virtual void FireEvent(object key, EventArgs e) {
  Delegate handler = (Delegate)listEventDelegates[key];
  if (handler != null) {
    handler.DynamicInvoke(new object[] {this, e});
  }
}

One of the limitations is that it assumes the delegate uses the standard (sender/args) method signature.  If a different signature is required, simply use your own implementation of the Onxxxx method.  Another is that overriding this method to perform an action against a specific event requires you to check the "key" parameter to ensure the event is the desired one.

Joe Martin
Minor Detail
One important step that was overlooked in the example is that during the OnMouseDown/Up methods, one must perform the following check before invoking the MouseEventHandler after retrieving it from the listEventDelegates instance:

  // Existing code
   MouseEventHandler mouseEventDelegate =
     (MouseEventHandler)listEventDelegates[mouseDownEventKey];

  // New code
  if (mouseEventDelegate != null) {
      mouseEventDelegate(this, e);
  }

Joe Martin