Use the built-in EventHandler delegate : Event : GUI Windows Forms C# Examples


C# Examples » GUI Windows Forms » Event »

 

Use the built-in EventHandler delegate









    
using  System;  
  
//  Declare  an  event  class.  
class  MyEvent  {  
    public  event  EventHandler  SomeEvent;  //  uses  EventHandler  delegate  
  
    //  This  is  called  to  fire  SomeEvent.  
    public  void  OnSomeEvent()  {  
        if(SomeEvent  !=  null)  
            SomeEvent(this,  EventArgs.Empty);  
    }  
}  
  
class  MainClass  {  
    static  void  handler(object  source,  EventArgs  arg)  {  
        Console.WriteLine("Event  occurred");  
        Console.WriteLine("Source  is  "  +  source);  
    }  
  
    public  static  void  Main()  {    
        MyEvent  evt  =  new  MyEvent();  
  
        //  Add  handler()  to  the  event  list.  
        evt.SomeEvent  +=  handler;  
  
        //  Fire  the  event.  
        evt.OnSomeEvent();  
    }  
}
    
   
  
   



Output

Event occurred
Source is MyEvent


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo GUI Windows Forms
» Event