Condividi tramite


Procedura: gestire più eventi mediante le relative proprietà

Per utilizzare le proprietà evento (eventi personalizzati in Visual Basic 2005), è necessario definire tali proprietà nella classe che genera gli eventi e quindi impostarne i delegati nelle classi che gestiscono gli eventi. Per implementare più proprietà evento in una classe, la classe deve archiviare internamente e mantenere il delegato definito per ogni evento. Uno degli approcci più comuni consiste nell'implementare un insieme di delegati indicizzato da una chiave evento.

Per archiviare i delegati per ogni evento, è possibile utilizzare la classe EventHandlerList o implementare il proprio insieme. La classe dell'insieme deve fornire metodi per impostare, accedere e recuperare il delegato del gestore eventi basato sulla chiave evento. È possibile, ad esempio, utilizzare una classe Hashtable o derivare una classe personalizzata dalla classe DictionaryBase. I dettagli sull'implementazione dell'insieme di delegati non devono essere esposti al di fuori della classe.

Ogni proprietà evento all'interno della classe definisce un metodo della funzione di accesso Add e della funzione di accesso Remove. La funzione di accesso Add di una proprietà aggiunge un'istanza del delegato di input all'insieme di delegati. La funzione di accesso Remove di una proprietà rimuove un'istanza del delegato di input dall'insieme di delegati. Le funzioni di accesso alle proprietà evento utilizzano la chiave predefinita della proprietà per aggiungere e rimuovere istanze dall'insieme di delegati.

Per gestire più eventi mediante le proprietà evento

  1. Definire un insieme di delegati all'interno della classe che genera gli eventi.

  2. Definire una chiave per ciascun evento.

  3. Definire le proprietà degli eventi nella classe che genera gli eventi.

  4. Utilizzare l'insieme di delegati per implementare i metodi delle funzioni di accesso Add e Remove per le proprietà evento.

  5. Utilizzare le proprietà evento pubbliche per aggiungere e rimuovere i delegati del gestore eventi nelle classi che gestiscono gli eventi.

Esempio

Nell'esempio C# seguente vengono implementate le proprietà evento MouseDown e MouseUp mediante un oggettoEventHandlerList per archiviare il delegato di ogni evento. Le parole chiave dei costrutti delle proprietà evento sono riportate in grassetto.

NotaNota

Le proprietà evento non sono supportate in Visual Basic 2005.

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

Vedere anche

Attività

Procedura: dichiarare eventi personalizzati per proteggere la memoria

Riferimenti

System.ComponentModel.EventHandlerList

System.Web.UI.Control.Events

Concetti

Generazione di più eventi

Altre risorse

Gestione e generazione di eventi