Events : Event : GUI Windows Forms C# Examples


C# Examples » GUI Windows Forms » Event »

 

Events









    
using  System;

public  class  Button
{
        public  delegate  void  ClickHandler(object  sender,  EventArgs  e);
        private  ClickHandler  click;
        
        public  void  AddClick(ClickHandler  clickHandler)
        {
                click  +=  clickHandler;
        }
        
        public  void  RemoveClick(ClickHandler  clickHandler)
        {
                click  -=  clickHandler;
        }
        
        protected  void  OnClick()
        {
                if  (click  !=  null)
                click(this,  null);
                
        }
        
        public  void  DoClick()
        {
                OnClick();
        }
}

class  MainClass
{
        static  public  void  ButtonHandler(object  sender,  EventArgs  e)
        {
                Console.WriteLine("Button  clicked");
        }
        
        public  static  void  Main()
        {
                Button  button  =  new  Button();
                
                button.AddClick(new  Button.ClickHandler(ButtonHandler));
                
                button.DoClick();
                
                button.RemoveClick(new  Button.ClickHandler(ButtonHandler));
                
                button.DoClick();
        }
}
    
   
  
   



Output

Button clicked


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo GUI Windows Forms
» Event