Export (0) Print
Expand All

EventArgs Class

EventArgs is the base class for classes containing event data.

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

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class EventArgs
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class EventArgs
SerializableAttribute 
ComVisibleAttribute(true) 
public class EventArgs
Not applicable.

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 example, the System.AssemblyLoadEventArgs class is used to hold the data for assembly load events, and contains a System.Reflection.Assembly that describes the loaded assembly.

For more information about events, see the EventHandler topic.

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.

using namespace System;

// FireEventArgs: a custom event inherited from EventArgs.
public ref class FireEventArgs: public 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.  
   String^ room;
   int ferocity;
};


//end of class FireEventArgs
// Class with a function that creates the eventargs and initiates the event
public ref class FireAlarm
{
public:
   delegate void FireEventHandler(    // Events are handled with delegates, so we must establish a FireEventHandler
   // as a delegate:
   Object^ sender, FireEventArgs^ fe );
   event FireEventHandler^ FireEvent;

   // Now, create a public event "FireEvent" whose type is our FireEventHandler delegate. 
   // This will be the starting point of our event-- it will create FireEventArgs,
   // and then raise the event, passing FireEventArgs. 
   void ActivateFireAlarm( String^ room, int ferocity )
   {
      FireEventArgs^ fireArgs = gcnew 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 );
   }

};


// end of class FireAlarm
// Class which handles the event
ref class FireHandlerClass
{
public:

   // Create a FireAlarm to handle and raise the fire events. 
   FireHandlerClass( FireAlarm^ fireAlarm )
   {
      
      // Add a delegate containing the ExtinguishFire function to the class'
      // event so that when FireAlarm is raised, it will subsequently execute 
      // ExtinguishFire.
      fireAlarm->FireEvent += gcnew FireAlarm::FireEventHandler( this, &FireHandlerClass::ExtinguishFire );
   }


   // This is the function to be executed when a fire event is raised. 
   void ExtinguishFire( Object^ sender, FireEventArgs^ fe )
   {
      Console::WriteLine( "\nThe ExtinguishFire function was called by {0}.", sender );
      
      // Now, act in response to the event.
      if ( fe->ferocity < 2 )
            Console::WriteLine( "This fire in the {0} is no problem.  I'm going to pour some water on it.", fe->room );
      else
      if ( fe->ferocity < 5 )
            Console::WriteLine( "I'm using FireExtinguisher to put out the fire in the {0}.", fe->room );
      else
            Console::WriteLine( "The fire in the {0} is out of control.  I'm calling the fire department!", fe->room );
   }

};


//end of class FireHandlerClass
int main()
{
   
   // Create an instance of the class that will be firing an event.
   FireAlarm^ myFireAlarm = gcnew 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 = gcnew FireHandlerClass( myFireAlarm );
   
   //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 );
} //end of main



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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft