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
Define a public event member in your class. Set the type of the event member to a System.EventHandler delegate.
public class Countdown { ... public event EventHandler CountdownCompleted; }
Public Class Countdown ... Public Event CountdownCompleted As EventHandler End Class
Provide a protected method in your class that raises the event. Name the method OnEventName. Raise the event within the method.
public class Countdown { ... public event EventHandler CountdownCompleted; protected virtual void OnCountdownCompleted(EventArgs e) { if (CountdownCompleted != null) CountdownCompleted(this, e); } }
Public Class Countdown ... Public Event CountdownCompleted As EventHandler Protected Overridable Sub OnCountdownCompleted(e As EventArgs) RaiseEvent CountdownCompleted(Me, e) End Sub End Class
Determine when to raise the event in your class. Call OnEventName to raise the event.
public class Countdown { ... public void Decrement { internalCounter = internalCounter - 1; if (internalCounter == 0) OnCountdownCompleted(new EventArgs()); } }
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
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.
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{ ... } }
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
Declare a delegate for the event. Name the delegate EventNameEventHandler.
public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);
Public Delegate Sub AlarmEventHandler(sender As Object, e As AlarmEventArgs)
Define a public event member named EventName in your class. Set the type of the event member to the event delegate type.
public class AlarmClock { ... public event AlarmEventHandler Alarm; }
Public Class AlarmClock ... Public Event Alarm As AlarmEventHandler End Class
Define a protected method in your class that raises the event. Name the method OnEventName. Raise the event within the method.
public class AlarmClock { ... public event AlarmHandler Alarm; protected virtual void OnAlarm(AlarmEventArgs e) { if (Alarm != null) Alarm(this, e); } }
Public Class AlarmClock ... Public Event Alarm As AlarmEventHandler Protected Overridable Sub OnAlarm(e As AlarmEventArgs) RaiseEvent Alarm(Me, e) End Sub End Class
Determine when to raise the event in your class. Call OnEventName to raise the event and pass in the event-specific data using EventNameEventArgs.
Public Class AlarmClock { ... public void Start() { ... System.Threading.Thread.Sleep(300); AlarmEventArgs e = new AlarmEventArgs(false, 0); OnAlarm(e); } }
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
See Also
Tasks
How to: Raise and Consume Events
Concepts
Events and Delegates
Raising an Event