Export (0) Print
Expand All
6 out of 31 rated this helpful - Rate this topic

EventArgs Class

EventArgs is the base class for classes containing event data.

For a list of all members of this type, see EventArgs Members.

System.Object
   System.EventArgs
      Derived classes

[Visual Basic]
<Serializable>
Public Class EventArgs
[C#]
[Serializable]
public class EventArgs
[C++]
[Serializable]
public __gc class EventArgs
[JScript]
public
   Serializable
class EventArgs

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.

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 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 Events Tutorial and EventHandler topics.

Example

[Visual Basic, C#, C++] The following code sample illustrates the use of EventArgs.

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Imports System

 _

' FireEventArgs: a custom event inherited from EventArgs.

Public Class FireEventArgs
   Inherits EventArgs
   
   Public Sub New(room As String, 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 'FireEventArgs
 _ 
'end of class FireEventArgs

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

   Delegate Sub FireEventHandler(sender As Object, 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(room As String, 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 'ActivateFireAlarm
End Class 'FireAlarm

' The following event will be the EventHandler.
 
Class FireHandlerClass
   
   
   ' Create a FireAlarm to handle and raise the fire events. 

   Public Sub New(fireAlarm As FireAlarm)
      
      ' 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

   End Sub 'New
   
   
   ' This is the function to be executed when a fire event is raised.
   
   Sub ExtinguishFire(sender As Object, fe As FireEventArgs)
     
      Console.WriteLine() 
      Console.WriteLine("The ExtinguishFire function was called by {0}.", sender.ToString())
      
      ' Now, act in response to the event.

      If fe.ferocity < 2 Then
         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 Then
            Console.WriteLine("Using Fire Extinguisher 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 If
      End If 'end of class FireHandlerClass
   End Sub 'ExtinguishFire
End Class 'FireHandlerClass

Public Class FireEventTest
   
   Public Shared Sub Main()
      
      ' 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)
        
      ' 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 'Main 'end of main
End Class 'FireEventTest ' end of FireEventTest

[C#] 

using System;

// 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;

}    //end of class FireEventArgs


// 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); 
    }
}    // end of class FireAlarm


// Class which handles the event

class FireHandlerClass {

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

    public 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 += new FireAlarm.FireEventHandler(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.ToString());

        // 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

public class FireEventTest {
    public static void Main ()     {    

        // 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);
        
        //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;

    }    //end of main

}    // end of FireEventTest


[C++] 

#using <mscorlib.dll>
using namespace System;

// FireEventArgs: a custom event inherited from EventArgs.

public __gc 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 __gc 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. 

   __event FireEventHandler* FireEvent;    

   // 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 = 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); 
   }
};    // end of class FireAlarm


// Class which handles the event

__gc class FireHandlerClass {

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

public:
   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 += new 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(S"\nThe ExtinguishFire function was called by {0}.", sender);

      // Now, act in response to the event.

      if (fe->ferocity < 2)
         Console::WriteLine(
            S"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(
            S"I'm using FireExtinguisher to put out the fire in the {0}.",
            fe->room);
      else 
         Console::WriteLine(
            S"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 = 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);

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

}    //end of main


[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

EventArgs Members | System Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.