The event function is provided by three interconnected elements: providing class, event delegate, and classes of events. The .NET Framework has an agreement with a class and method related to the event. If you want your class to trigger an event called EventName, you need the following elements.
The class of incident data is named EventNameEventArgs. This class must be from
System.eventargs
Export. The commission of events is called EventNameEventHandler. Start the class of events. This class must provide:
Event declaration. [C #]
Public Event EventNameEventHandler EventName; [Visual Basic]
Public Event EventName as eventnameEventHandler triggered an event method called OneventName.
The .NET Framework class library or third-party library may have defined event data classes and event commission classes. In this case, you don't need to define these classes.
Provide event functions define a class that provides event data. This class must export from System.Eventargs (which is the base class of event data). Examples are as follows. Note that this step is not required if there is an event data class that has existing events or data associated with your event. If there is no event data, use the base class System.EventArgs. [C #] public class AlarmEventArgs: EventArgs {private readonly int nrings = 0; private readonly bool snoozePressed = false; // 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 '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 Declaration Event Entrusted, as shown below. Note If the event does not generate data, you do not need to declare a custom delegate. In this case, use the base event handler system.componentmodel.eventhandler. [C #] public delegate void AlarmEventHandler (object sender, AlarmEventArgs e); [Visual Basic] Public Delegate Sub AlarmEventHandler (sender As Object, e As AlarmEventArgs) using the event keyword (whose type is an event delegate) your class is defined in a Public event members, as shown below. [C #] public class AlarmClock {... public event AlarmEventHandler Alarm;} delegate [Visual Basic] Public Class AlarmClock ... Public Event Alarm As AlarmEventHandlerEnd Class in AlarmClock class, Alarm events are AlarmEventHandler type. When the compiler encounters an Event keyword, it creates a private member, such as [C #] private alarmEventhandler al = NULL; and ADD_ALARM and REMOVE_ALARM two public methods. These methods are event hooks that allow the commission and event commissioning Al to merge or remove from the part. These details are hidden on the programmer. Note In other languages other than C # and Visual Basic .NET, the compiler may not automatically generate code corresponding to event members, and you may need to explicitly define event hooks and private delegate fields. Provide a protected method in the class that causes events.
The name of this method must be on atname. Oneventname method triggers events by calling commission. The final code example of this topic shows the implementation of OneventName. Note that the protected OneventName method also allows to rewrite events without having to attach it to them. The derived class must always call the list of oneventname methods to ensure that the registered delegate receives events. [C #] public class AlarmClock {... public event AlarmHandler Alarm; protected virtual void OnAlarm (AlarmEvent e) {...}} [Visual Basic] Public Class AlarmClock ... Public Event Alarm As AlarmEventHandler Protected Overridable Sub OnAlarm (e AS ALARMEVENTARGS ... END SUBEND CLASS The following code snippet puts all the elements discussed in this section together. For a complete example of implementing and using an event, see the event example. [C #] // Step 1. Class that defines data for the event // public class AlarmEventArgs: EventArgs {private readonly bool snoozePressed = false; private readonly int nrings = 0; // Constructor public AlarmEventArgs (bool snoozePressed, int nrings). {...} // Properties public int NumRings {get {return nrings;}}. public bool snoozePressed {get {return snoozePressed;}} public string AlarmText {get {...}}} // Step 2. Delegate declaration ./public delegate Void AlarMeventHendler (Object Sender, AlarmEventArgs E);
// Class definition.//public class AlarmClock {// Step 3. The Alarm event is defined using the event keyword.//The type of Alarm is AlarmEventHandler public event AlarmEventHandler Alarm;. //// Step 4. The protected OnAlarm method raises the event by invoking // the delegates. The sender is always this, the current instance of // the class.// protected virtual void OnAlarm (AlarmEventArgs e) {if (Alarm! = null) {// Invokes the delegates . Alarm (this, e);}}} [Visual Basic] 'Step 1. Class that defines data for the event'Public Class AlarmEventArgs Inherits EventArgs Private _snoozePressed As Boolean = False Private nrings As Integer = 0' Constructor Public Sub New. (snoozePressed As Boolean, nrings As Integer) ... End Sub 'Properties. Public ReadOnly Property NumRings () As Integer Get Return nrings End Get End Property Public ReadOnly Property SnoozePressed () As Boolean Get Return _snoozePressed End Get End Property Public Readonly Property AlarmText () AS String Get ... End Get End Propertyend CLASS
'Step 2. Delegate Declaration.'Public Delegate Sub AlarMeventhandler (Sender As Object, e as alarmeventargs)