Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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.

Example

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.

NoteNote

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); }
   }

   // 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); }
   }
}

See Also

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft