Freigeben über


EventArgs Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

EventArgs is the base class for classes containing event data.

Inheritance Hierarchy

System.Object
  System.EventArgs
    More...

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Class EventArgs
[ComVisibleAttribute(true)]
public class EventArgs

The EventArgs type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 EventArgs Initializes a new instance of the EventArgs class.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Fields

  Name Description
Public fieldStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Empty Represents an event with no event data.

Top

Remarks

This class contains no event data; it is used by events that do not pass state information to an event handler when an event is raised. If the event handler requires state information, the application must derive a class from this class to hold the data.

For more information about events, see the EventHandler topic.

Examples

The following code sample illustrates the use of EventArgs.

In this sample, FireEventArgs is a set of event arguments derived from EventArgs, and passed to the FireEventHandler when an event is raised by calling ActivateFireAlarm.

Imports System.Windows.Controls

' FireEventArgs: a custom event inherited from EventArgs.

Public Class FireEventArgs
    Inherits EventArgs

    Public Sub New(ByVal room As String, ByVal ferocity As Integer)
        Me.room = room
        Me.ferocity = ferocity
    End Sub 'New

    ' The fire event will have two pieces of information-- 
    ' 1) Where the fire is, and 2) how "ferocious" it is. 

    Public room As String
    Public ferocity As Integer
End Class


Public Class FireAlarm

    ' Events are handled with delegates, so we must establish a FireEventHandler
    ' as a delegate:

    Delegate Sub FireEventHandler(ByVal sender As Object, ByVal fe As FireEventArgs)

    ' Now, create a public event "FireEvent" whose type is our FireEventHandler delegate.

    Public Event FireEvent As FireEventHandler


    ' This will be the starting point of our event-- it will create FireEventArgs,
    ' and then raise the event, passing FireEventArgs.

    Public Sub ActivateFireAlarm(ByVal room As String, ByVal ferocity As Integer)

        Dim fireArgs As New FireEventArgs(room, ferocity)

        '  Now, raise the event by invoking the delegate. Pass in 
        '  the object that initated the event (Me) as well as FireEventArgs. 
        '  The call must match the signature of FireEventHandler.

        RaiseEvent FireEvent(Me, fireArgs)

    End Sub
End Class

' The following event will be the EventHandler.

Class FireHandlerClass

    Private Shared _TextBlock As TextBlock


    ' Create a FireAlarm to handle and raise the fire events. 

    Public Sub New(ByVal fireAlarm As FireAlarm, ByVal outputBlock As TextBlock)

        ' Add a delegate containing the ExtinguishFire function to the class'
        ' event so that when FireAlarm is raised, it will subsequently execute 
        ' ExtinguishFire.

        AddHandler fireAlarm.FireEvent, AddressOf ExtinguishFire

        FireHandlerClass._TextBlock = outputBlock

    End Sub


    ' This is the function to be executed when a fire event is raised.

    Sub ExtinguishFire(ByVal sender As Object, ByVal fe As FireEventArgs)

        _TextBlock.Text &= vbCrLf
        _TextBlock.Text += String.Format("The ExtinguishFire function was called by {0}.", sender.ToString()) & vbCrLf

        ' Now, act in response to the event.

        If fe.ferocity < 2 Then
            _TextBlock.Text += String.Format("This fire in the {0} is no problem.  I'm going to pour some water on it.", fe.room) & vbCrLf
        Else
            If fe.ferocity < 5 Then
                _TextBlock.Text += String.Format("Using Fire Extinguisher to put out the fire in the {0}.", fe.room) & vbCrLf
            Else
                _TextBlock.Text += String.Format("The fire in the {0} is out of control.  I'm calling the fire department.", fe.room) & vbCrLf
            End If
        End If
    End Sub
End Class

Public Class Example

    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

        ' Create an instance of the class that will be raising the event.
        Dim myFireAlarm As New FireAlarm()

        ' Create an instance of the class that will be handling the event. Note that 
        ' it receives the class that will fire the event as a parameter. 

        Dim myFireHandler As New FireHandlerClass(myFireAlarm, outputBlock)

        ' Now, use the FireAlarm class to raise a few events. 

        myFireAlarm.ActivateFireAlarm("Kitchen", 3)
        myFireAlarm.ActivateFireAlarm("Study", 1)
        myFireAlarm.ActivateFireAlarm("Porch", 5)

        Return
    End Sub
End Class
using System;
using System.Windows.Controls;

// FireEventArgs: a custom event inherited from EventArgs.

public class FireEventArgs : EventArgs
{
    public FireEventArgs(string room, int ferocity)
    {
        this.room = room;
        this.ferocity = ferocity;
    }

    // The fire event will have two pieces of information--
    // 1) Where the fire is, and 2) how "ferocious" it is.

    public string room;
    public int ferocity;

}


// Class with a function that creates the eventargs and initiates the event
public class FireAlarm
{

    // Events are handled with delegates, so we must establish a FireEventHandler
    // as a delegate:

    public delegate void FireEventHandler(object sender, FireEventArgs fe);

    // Now, create a public event "FireEvent" whose type is our FireEventHandler delegate.

    public event FireEventHandler FireEvent;

    // This will be the starting point of our event-- it will create FireEventArgs,
    // and then raise the event, passing FireEventArgs.

    public void ActivateFireAlarm(string room, int ferocity)
    {

        FireEventArgs fireArgs = new FireEventArgs(room, ferocity);

        // Now, raise the event by invoking the delegate. Pass in
        // the object that initated the event (this) as well as FireEventArgs.
        // The call must match the signature of FireEventHandler.

        FireEvent(this, fireArgs);
    }
}


// Class which handles the event

class FireHandlerClass
{

    private static TextBlock _textblock;


    // Create a FireAlarm to handle and raise the fire events.

    public FireHandlerClass(FireAlarm fireAlarm, TextBlock outputBlock)
    {

        // Add a delegate containing the ExtinguishFire function to the class'
        // event so that when FireAlarm is raised, it will subsequently execute
        // ExtinguishFire.

        fireAlarm.FireEvent += new FireAlarm.FireEventHandler(ExtinguishFire);

        FireHandlerClass._textblock = outputBlock;

    }

    // This is the function to be executed when a fire event is raised.

    void ExtinguishFire(object sender, FireEventArgs fe)
    {

        _textblock.Text += String.Format("\nThe ExtinguishFire function was called by {0}.", sender.ToString()) + "\n";

        // Now, act in response to the event.

        if (fe.ferocity < 2)
            _textblock.Text += String.Format("This fire in the {0} is no problem.  I'm going to pour some water on it.", fe.room) + "\n";
        else if (fe.ferocity < 5)
            _textblock.Text += String.Format("I'm using FireExtinguisher to put out the fire in the {0}.", fe.room) + "\n";
        else
            _textblock.Text += String.Format("The fire in the {0} is out of control.  I'm calling the fire department!", fe.room) + "\n";
    }
}

public class Example
{
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {

        // Create an instance of the class that will be firing an event.

        FireAlarm myFireAlarm = new FireAlarm();

        // Create an instance of the class that will be handling the event. Note that
        // it receives the class that will fire the event as a parameter.

        FireHandlerClass myFireHandler = new FireHandlerClass(myFireAlarm, outputBlock);

        //use our class to raise a few events and watch them get handled
        myFireAlarm.ActivateFireAlarm("Kitchen", 3);
        myFireAlarm.ActivateFireAlarm("Study", 1);
        myFireAlarm.ActivateFireAlarm("Porch", 5);

        return;

    }

}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Inheritance Hierarchy

System.Object
  System.EventArgs
    System.Collections.Specialized.NotifyCollectionChangedEventArgs
    System.ComponentModel.AsyncCompletedEventArgs
    System.ComponentModel.CancelEventArgs
    System.ComponentModel.Composition.Hosting.ComposablePartCatalogChangeEventArgs
    System.ComponentModel.Composition.Hosting.ExportsChangeEventArgs
    System.ComponentModel.CurrentChangingEventArgs
    System.ComponentModel.DataErrorsChangedEventArgs
    System.ComponentModel.DoWorkEventArgs
    System.ComponentModel.ProgressChangedEventArgs
    System.ComponentModel.PropertyChangedEventArgs
    System.ComponentModel.PropertyChangingEventArgs
    System.Data.Services.Client.ReadingWritingEntityEventArgs
    System.Data.Services.Client.SendingRequestEventArgs
    System.Net.Sockets.SocketAsyncEventArgs
    System.Net.WriteStreamClosedEventArgs
    System.ResolveEventArgs
    System.Runtime.InteropServices.Automation.AutomationEventArgs
    System.ServiceModel.UnknownMessageReceivedEventArgs
    System.Threading.Tasks.UnobservedTaskExceptionEventArgs
    System.UnhandledExceptionEventArgs
    System.Windows.ApplicationUnhandledExceptionEventArgs
    System.Windows.Browser.HtmlEventArgs
    System.Windows.CheckAndDownloadUpdateCompletedEventArgs
    System.Windows.Controls.DataGridCellEditEndedEventArgs
    System.Windows.Controls.DataGridColumnEventArgs
    System.Windows.Controls.DataGridPreparingCellForEditEventArgs
    System.Windows.Controls.DataGridRowClipboardEventArgs
    System.Windows.Controls.DataGridRowDetailsEventArgs
    System.Windows.Controls.DataGridRowEditEndedEventArgs
    System.Windows.Controls.DataGridRowEventArgs
    System.Windows.Controls.DataGridRowGroupHeaderEventArgs
    System.Windows.Controls.DatePickerDateValidationErrorEventArgs
    System.Windows.Controls.DrawEventArgs
    System.Windows.Controls.FocusingInvalidControlEventArgs
    System.Windows.Controls.NotifyEventArgs
    System.Windows.Controls.Pivot.CxmlCollectionStateChangedEventArgs
    System.Windows.Controls.Pivot.PivotViewerCommandsRequestedEventArgs
    System.Windows.Controls.Pivot.PivotViewerFilterEventArgs
    System.Windows.Controls.Pivot.PivotViewerItemDoubleClickEventArgs
    System.Windows.Controls.Pivot.PivotViewerLinkEventArgs
    System.Windows.Controls.Pivot.PivotViewerViewUpdatingEventArgs
    System.Windows.Controls.Primitives.ItemsChangedEventArgs
    System.Windows.Data.FilterEventArgs
    System.Windows.Graphics.RenderModeChangedEventArgs
    System.Windows.Input.TouchFrameEventArgs
    System.Windows.Interop.NavigationStateChangedEventArgs
    System.Windows.Media.Imaging.DownloadProgressEventArgs
    System.Windows.Media.RenderingEventArgs
    System.Windows.Messaging.MessageReceivedEventArgs
    System.Windows.Navigation.FragmentNavigationEventArgs
    System.Windows.Navigation.NavigationEventArgs
    System.Windows.Navigation.NavigationFailedEventArgs
    System.Windows.Printing.BeginPrintEventArgs
    System.Windows.Printing.EndPrintEventArgs
    System.Windows.Printing.PrintPageEventArgs
    System.Windows.RoutedEventArgs
    System.Windows.StartupEventArgs
    System.Windows.VisualStateChangedEventArgs
    System.Xml.Linq.XObjectChangeEventArgs