Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
 How to: Implement Events in Your Cl...
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Developer's Guide
How to: Implement Events in Your Class

The following procedures describe how to implement an event in a class. The first procedure implements an event that does not have associated data; it uses the classes System..::.EventArgs and System..::.EventHandler for the event data and delegate handler. The second procedure implements an event with custom data; it defines custom classes for the event data and the event delegate handler.

For a complete sample that illustrates raising and handling events, see How to: Raise and Consume Events.

To implement an event without event-specific data

  1. Define a public event member in your class. Set the type of the event member to a System..::.EventHandler delegate.

    C#
    public class Countdown 
    {
        ...
        public event EventHandler CountdownCompleted;   
    }
    

    Visual Basic
    Public Class Countdown
        ...
        Public Event CountdownCompleted As EventHandler
    End Class
  2. Provide a protected method in your class that raises the event. Name the method OnEventName. Raise the event within the method.

    C#
    public class Countdown 
    {
        ...
        public event EventHandler CountdownCompleted;   
        protected virtual void OnCountdownCompleted(EventArgs e)
        {
            if (CountdownCompleted != null)
                CountdownCompleted(this, e);
        }
    }
    

    Visual Basic
    Public Class Countdown
       ...
       Public Event CountdownCompleted As EventHandler
       Protected Overridable Sub OnCountdownCompleted(e As EventArgs)
          RaiseEvent CountdownCompleted(Me, e)
       End Sub
    End Class
  3. Determine when to raise the event in your class. Call OnEventName to raise the event.

    C#
    public class Countdown 
    {
        ...
        public void Decrement
        {
            internalCounter = internalCounter - 1;
            if (internalCounter == 0)
                OnCountdownCompleted(new EventArgs());
        }
    }
    

    Visual Basic
    Public Class Countdown
        ...
        Public Function Decrement
            InternalCounter = internalCounter - 1
            If internalCounter = 0
                OnCountdownComplete(New EventArgs())
            End If
        End Function
    End Class

To implement an event with event-specific data

  1. Define a class that provides data for the event. Name the class EventNameArgs, derive the class from System..::.EventArgs, and add any event-specific members.

    C#
    public class AlarmEventArgs : EventArgs 
    {
       private readonly int nrings = 0;
       private readonly bool snoozePressed = false;
       
       //Constructor.
       public AlarmEventArgs(bool snoozePressed, int nrings) 
       {
          this.snoozePressed = snoozePressed;
          this.nrings = nrings;
       }
       
       //Properties.
       public string AlarmText {  
          ...
       }
       public int NumRings {
          ...
       }
       public bool SnoozePressed{
          ...
       }
    }
    

    Visual Basic
    Public Class AlarmEventArgs
       Inherits EventArgs
       Private nrings As Integer = 0
       Private _snoozePressed As Boolean = False
       
       'Constructor.
       Public Sub New(ByVal snoozePressed As Boolean, ByVal nrings As Integer) 
          Me.snoozePressed = snoozePressed
          Me.nrings = nrings
       End Sub
       
       'Properties.
       Public ReadOnly Property AlarmText() As String
          ...
       End Property 
       
       Public ReadOnly Property NumRings() As Integer
          ...
       End Property 
       
       Public ReadOnly Property SnoozePressed() As Boolean
          ...
       End Property
    End Class
    
  2. Declare a delegate for the event. Name the delegate EventNameEventHandler.

    C#
    public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);
    

    Visual Basic
    Public Delegate Sub AlarmEventHandler(sender As Object, e As AlarmEventArgs)
    
  3. Define a public event member named EventName in your class. Set the type of the event member to the event delegate type.

    C#
    public class AlarmClock 
    {
        ...
        public event AlarmEventHandler Alarm;
    }
    

    Visual Basic
    Public Class AlarmClock
        ...
        Public Event Alarm As AlarmEventHandler
    End Class
  4. Define a protected method in your class that raises the event. Name the method OnEventName. Raise the event within the method.

    C#
    public class AlarmClock 
    {
        ...
        public event AlarmHandler Alarm;
        protected virtual void OnAlarm(AlarmEventArgs e)
        {
          if (Alarm != null) 
              Alarm(this, e); 
        }
    }
    

    Visual Basic
    Public Class AlarmClock
        ...
        Public Event Alarm As AlarmEventHandler
        Protected Overridable Sub OnAlarm(e As AlarmEventArgs)
            RaiseEvent Alarm(Me, e)
        End Sub
    End Class
  5. Determine when to raise the event in your class. Call OnEventName to raise the event and pass in the event-specific data using EventNameEventArgs.

    C#
    Public Class AlarmClock
    {
        ...
        public void Start()
        {
            ...
            System.Threading.Thread.Sleep(300);
            AlarmEventArgs e = new AlarmEventArgs(false, 0);
            OnAlarm(e);
        }
    }
    

    Visual Basic
    Public Class AlarmClock
        ...
        Public Function Start
            ...
            System.Threading.Thread.Sleep(300)
            Dim e As AlarmEventArgs = New AlarmEventArgs(False, 0)
            OnAlarm(e)
        End Function
    End Class
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content      
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker