다음을 통해 공유


EventArgs 클래스

EventArgs는 이벤트 데이터가 들어 있는 클래스에 대한 기본 클래스입니다.

네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class EventArgs
‘사용 방법
Dim instance As EventArgs
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class EventArgs
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class EventArgs
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class EventArgs
SerializableAttribute 
ComVisibleAttribute(true) 
public class EventArgs

설명

이 클래스에는 이벤트 데이터가 없으므로 이벤트가 발생할 때 이벤트 처리기에 대한 상태 정보를 전달하지 않는 이벤트에서 사용됩니다. 이벤트 처리기에 상태 정보가 필요한 경우, 응용 프로그램에서는 이 클래스에서 클래스를 파생시켜 해당 데이터를 보관해야 합니다.

예를 들어, System.AssemblyLoadEventArgs 클래스는 어셈블리 로드 이벤트에 대한 데이터를 보관하는 데 사용되며, 로드된 어셈블리를 설명하는 System.Reflection.Assembly를 포함합니다.

이벤트에 대한 자세한 내용은 EventHandler 항목을 참조하십시오.

예제

다음 코드 예제에서는 EventArgs를 사용합니다.

이 예제에서 FireEventArgs는 EventArgs에서 파생되고 ActivateFireAlarm을 호출해서 이벤트가 발생할 때 FireEventHandler로 전달되는 이벤트 인수의 집합입니다.

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

상속 계층 구조

System.Object
  System.EventArgs
     파생 클래스

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

Windows 98, Windows 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

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

EventArgs 멤버
System 네임스페이스