如何:使用事件屬性處理多個事件How to: Handle Multiple Events Using Event Properties

為了要使用事件屬性,您會在引發事件的類別中定義事件屬性,然後在處理事件的類別中設定事件屬性的委派。To use event properties, 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.

若要儲存每個事件的委派,您可以使用 EventHandlerList 類別,或實作您自己的集合。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. 例如,您可以使用 Hashtable 類別,或是從 DictionaryBase 類別衍生自訂類別。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.

類別內的每個事件屬性,都會定義一個 add 存取子方法和一個 remove 存取子方法。Each event property within the class defines an add accessor method and a remove accessor method. 事件屬性的 add 存取子會在委派集合中加入輸入委派執行個體。The add accessor for an event property adds the input delegate instance to the delegate collection. 事件屬性的 remove 存取子會從委派集合中移除輸入委派執行個體。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. 使用委派集合實作事件屬性的 add 和 remove 存取子方法。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

下列 C# 範例會使用 EventHandlerList 實作事件屬性 MouseDownMouseUp,以儲存每個事件的委派。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.

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

    // Define the delegate collection.
protected:
    EventHandlerList^ listEventDelegates;

private:
    // Define a unique key for each event.
    static Object^ mouseDownEventKey = gcnew Object();
    static Object^ mouseUpEventKey = gcnew Object();

    // Define the MouseDown event property.
public:
    SampleControl()
    {
        listEventDelegates = gcnew EventHandlerList();
    }

    event MouseEventHandler^ MouseDown
    {
        // Add the input delegate to the collection.
        void add(MouseEventHandler^ value)
        {
            listEventDelegates->AddHandler(mouseDownEventKey, value);
        }
        // Remove the input delegate from the collection.
        void remove(MouseEventHandler^ value)
        {
            listEventDelegates->RemoveHandler(mouseDownEventKey, value);
        }
        // Raise the event with the delegate specified by mouseDownEventKey
        void raise(Object^ sender, MouseEventArgs^ e)
        {
            MouseEventHandler^ mouseEventDelegate =
                (MouseEventHandler^)listEventDelegates[mouseDownEventKey];
            mouseEventDelegate(sender, e);
        }
    }

    // Define the MouseUp event property.
    event MouseEventHandler^ MouseUp
    {
        // Add the input delegate to the collection.
        void add(MouseEventHandler^ value)
        {
            listEventDelegates->AddHandler(mouseUpEventKey, value);
        }
        // Remove the input delegate from the collection.
        void remove(MouseEventHandler^ value)
        {
            listEventDelegates->RemoveHandler(mouseUpEventKey, value);
        }
        // Raise the event with the delegate specified by mouseUpEventKey
        void raise(Object^ sender, MouseEventArgs^ e)
        {
            MouseEventHandler^ mouseEventDelegate =
                (MouseEventHandler^)listEventDelegates[mouseUpEventKey];
            mouseEventDelegate(sender, e);
        }
    }
};
// 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);
    }
}
' The class SampleControl defines two event properties, MouseUp and MouseDown.
Class SampleControl
    Inherits Component
    ' :
    ' Define other control methods and properties.
    ' :

    ' Define the delegate collection.
    Protected listEventDelegates As New EventHandlerList()

    ' Define a unique key for each event.
    Shared ReadOnly mouseDownEventKey As New Object()
    Shared ReadOnly mouseUpEventKey As New Object()

    ' Define the MouseDown event property.
    Public Custom Event MouseDown As MouseEventHandler
        ' Add the input delegate to the collection.
        AddHandler(Value As MouseEventHandler)
            listEventDelegates.AddHandler(mouseDownEventKey, Value)
        End AddHandler
        ' Remove the input delegate from the collection.
        RemoveHandler(Value As MouseEventHandler)
            listEventDelegates.RemoveHandler(mouseDownEventKey, Value)
        End RemoveHandler
        ' Raise the event with the delegate specified by mouseDownEventKey
        RaiseEvent(sender As Object, e As MouseEventArgs)
            Dim mouseEventDelegate As MouseEventHandler = _
                listEventDelegates(mouseDownEventKey)
            mouseEventDelegate(sender, e)
        End RaiseEvent
    End Event

    ' Define the MouseUp event property.
    Public Custom Event MouseUp As MouseEventHandler
        ' Add the input delegate to the collection.
        AddHandler(Value As MouseEventHandler)
            listEventDelegates.AddHandler(mouseUpEventKey, Value)
        End AddHandler
        ' Remove the input delegate from the collection.
        RemoveHandler(Value As MouseEventHandler)
            listEventDelegates.RemoveHandler(mouseUpEventKey, Value)
        End RemoveHandler
        ' Raise the event with the delegate specified by mouseDownUpKey
        RaiseEvent(sender As Object, e As MouseEventArgs)
            Dim mouseEventDelegate As MouseEventHandler = _
                listEventDelegates(mouseUpEventKey)
            mouseEventDelegate(sender, e)
        End RaiseEvent
    End Event
End Class

請參閱See also