次の方法で共有


方法: イベント プロパティを使用して複数のイベントを処理する

イベント プロパティを使用するには、イベントを発生させるクラスでイベント プロパティを定義し、イベントを処理するクラスのイベント プロパティのデリゲートを設定します。 クラスに複数のイベント プロパティを実装するには、各イベントに対して定義されたデリゲートを内部的に格納して維持する必要があります。 フィールドに似たイベントごとに、対応するバッキング フィールド参照型が生成されます。 これにより、イベントの数が増えると、不要な割り当てが発生する可能性があります。 別の方法として、一般的な方法は、キーごとにイベントを格納する EventHandlerList を維持することです。

各イベントのデリゲートを格納するには、 EventHandlerList クラスを使用するか、独自のコレクションを実装します。 コレクション クラスは、イベント キーに基づいてイベント ハンドラー デリゲートを設定、アクセス、および取得するためのメソッドを提供する必要があります。 たとえば、 Hashtable クラスを使用したり、 DictionaryBase クラスからカスタム クラスを派生させたりすることができます。 デリゲート コレクションの実装の詳細は、クラスの外部で公開する必要はありません。

クラス内の各イベント プロパティは、add アクセサー メソッドと remove アクセサー メソッドを定義します。 イベント プロパティの add アクセサーは、デリゲート コレクションに入力デリゲート インスタンスを追加します。 イベント プロパティの remove アクセサーは、デリゲート コレクションから入力デリゲート インスタンスを削除します。 イベント プロパティ アクセサーは、イベント プロパティの定義済みキーを使用して、デリゲート コレクションのインスタンスを追加および削除します。

イベント プロパティを使用して複数のイベントを処理するには

  1. イベントを発生させるデリゲート コレクションをクラス内で定義します。

  2. 各イベントのキーを定義します。

  3. イベントを発生させるクラスのイベント プロパティを定義します。

  4. デリゲート コレクションを使用して、イベント プロパティの追加および削除アクセサー メソッドを実装します。

  5. パブリック イベント プロパティを使用して、イベントを処理するクラスのイベント ハンドラー デリゲートを追加および削除します。

次の C# の例では、各イベントのデリゲートを格納するMouseDownを使用して、MouseUpEventHandlerListのイベント プロパティを実装します。 イベント プロパティコンストラクトのキーワードは太字です。

// 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

こちらも参照ください