EventArgs Klasse
Definition
Stellt die Basisklasse für Klassen dar, die Ereignisdaten enthalten, und stellt einen Wert für Ereignisse bereit, die keine Daten enthalten.Represents the base class for classes that contain event data, and provides a value to use for events that do not include event data.
public ref class EventArgs
public class EventArgs
[System.Serializable]
public class EventArgs
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class EventArgs
type EventArgs = class
[<System.Serializable>]
type EventArgs = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type EventArgs = class
Public Class EventArgs
- Vererbung
-
EventArgs
- Abgeleitet
- Attribute
Beispiele
Das folgende Beispiel zeigt eine benutzerdefinierte Ereignisdaten Klasse mit ThresholdReachedEventArgs
dem Namen, die von der-Klasse abgeleitet wird EventArgs .The following example shows a custom event data class named ThresholdReachedEventArgs
that derives from the EventArgs class. Eine Instanz der Ereignisdaten Klasse wird an den Ereignishandler für das-Ereignis übermittelt ThresholdReached
.An instance of the event data class is passed to the event handler for the ThresholdReached
event.
using namespace System;
public ref class ThresholdReachedEventArgs : public EventArgs
{
public:
property int Threshold;
property DateTime TimeReached;
};
public ref class Counter
{
private:
int threshold;
int total;
public:
Counter() {};
Counter(int passedThreshold)
{
threshold = passedThreshold;
}
void Add(int x)
{
total += x;
if (total >= threshold) {
ThresholdReachedEventArgs^ args = gcnew ThresholdReachedEventArgs();
args->Threshold = threshold;
args->TimeReached = DateTime::Now;
OnThresholdReached(args);
}
}
event EventHandler<ThresholdReachedEventArgs^>^ ThresholdReached;
protected:
virtual void OnThresholdReached(ThresholdReachedEventArgs^ e)
{
ThresholdReached(this, e);
}
};
public ref class SampleHandler
{
public:
static void c_ThresholdReached(Object^ sender, ThresholdReachedEventArgs^ e)
{
Console::WriteLine("The threshold of {0} was reached at {1}.",
e->Threshold, e->TimeReached);
Environment::Exit(0);
}
};
void main()
{
Counter^ c = gcnew Counter((gcnew Random())->Next(10));
c->ThresholdReached += gcnew EventHandler<ThresholdReachedEventArgs^>(SampleHandler::c_ThresholdReached);
Console::WriteLine("press 'a' key to increase total");
while (Console::ReadKey(true).KeyChar == 'a') {
Console::WriteLine("adding one");
c->Add(1);
}
}
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Counter c = new Counter(new Random().Next(10));
c.ThresholdReached += c_ThresholdReached;
Console.WriteLine("press 'a' key to increase total");
while (Console.ReadKey(true).KeyChar == 'a')
{
Console.WriteLine("adding one");
c.Add(1);
}
}
static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e)
{
Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached);
Environment.Exit(0);
}
}
class Counter
{
private int threshold;
private int total;
public Counter(int passedThreshold)
{
threshold = passedThreshold;
}
public void Add(int x)
{
total += x;
if (total >= threshold)
{
ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
args.Threshold = threshold;
args.TimeReached = DateTime.Now;
OnThresholdReached(args);
}
}
protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
{
EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
if (handler != null)
{
handler(this, e);
}
}
public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
}
public class ThresholdReachedEventArgs : EventArgs
{
public int Threshold { get; set; }
public DateTime TimeReached { get; set; }
}
}
Module Module1
Sub Main()
Dim c As Counter = New Counter(New Random().Next(10))
AddHandler c.ThresholdReached, AddressOf c_ThresholdReached
Console.WriteLine("press 'a' key to increase total")
While Console.ReadKey(True).KeyChar = "a"
Console.WriteLine("adding one")
c.Add(1)
End While
End Sub
Sub c_ThresholdReached(sender As Object, e As ThresholdReachedEventArgs)
Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached)
Environment.Exit(0)
End Sub
End Module
Class Counter
Private threshold As Integer
Private total As Integer
Public Sub New(passedThreshold As Integer)
threshold = passedThreshold
End Sub
Public Sub Add(x As Integer)
total = total + x
If (total >= threshold) Then
Dim args As ThresholdReachedEventArgs = New ThresholdReachedEventArgs()
args.Threshold = threshold
args.TimeReached = DateTime.Now
OnThresholdReached(args)
End If
End Sub
Protected Overridable Sub OnThresholdReached(e As ThresholdReachedEventArgs)
RaiseEvent ThresholdReached(Me, e)
End Sub
Public Event ThresholdReached As EventHandler(Of ThresholdReachedEventArgs)
End Class
Class ThresholdReachedEventArgs
Inherits EventArgs
Public Property Threshold As Integer
Public Property TimeReached As DateTime
End Class
Hinweise
Diese Klasse dient als Basisklasse für alle Klassen, die Ereignisdaten darstellen.This class serves as the base class for all classes that represent event data. Beispielsweise wird die System.AssemblyLoadEventArgs -Klasse von abgeleitet EventArgs und zum Speichern der Daten für assemblyauslastungs-Ereignisse verwendet.For example, the System.AssemblyLoadEventArgs class derives from EventArgs and is used to hold the data for assembly load events. Wenn Sie eine benutzerdefinierte Ereignisdaten Klasse erstellen möchten, erstellen Sie eine Klasse, die von der-Klasse abgeleitet wird, EventArgs und stellen Sie die Eigenschaften zum Speichern der erforderlichen Daten bereitTo create a custom event data class, create a class that derives from the EventArgs class and provide the properties to store the necessary data. Der Name der benutzerdefinierten Ereignisdaten Klasse sollte mit enden EventArgs
.The name of your custom event data class should end with EventArgs
.
Um ein Objekt zu übergeben, das keine Daten enthält, verwenden Sie das- Empty Feld.To pass an object that does not contain any data, use the Empty field.
Weitere Informationen zu Ereignissen finden Sie im Artikel Behandlung und Auswerfen von Ereignissen .For more information about events, see the Handling and Raising Events article.
Konstruktoren
EventArgs() |
Initialisiert eine neue Instanz der EventArgs-Klasse.Initializes a new instance of the EventArgs class. |
Felder
Empty |
Stellt einen zu verwendenden Wert für Ereignisse, die keine Ereignisdaten haben.Provides a value to use with events that do not have event data. |
Methoden
Equals(Object) |
Bestimmt, ob das angegebene Objekt gleich dem aktuellen Objekt ist.Determines whether the specified object is equal to the current object. (Geerbt von Object) |
GetHashCode() |
Fungiert als Standardhashfunktion.Serves as the default hash function. (Geerbt von Object) |
GetType() |
Ruft den Type der aktuellen Instanz ab.Gets the Type of the current instance. (Geerbt von Object) |
MemberwiseClone() |
Erstellt eine flache Kopie des aktuellen Object.Creates a shallow copy of the current Object. (Geerbt von Object) |
ToString() |
Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.Returns a string that represents the current object. (Geerbt von Object) |