Monday, 19 September 2011

how to add and remove event handers

simple with += and -= operators

/* A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson Publisher: Apress  L.P.  ISBN: 1-893115-62-3  */
// 23 - Events\Custom Add and Remove --- copyright 2000 Eric Gunnerson


using System;
using System.Collections;
using System.Runtime.CompilerServices;


public class EventsCustomAddandRemove
{
    static public void ButtonHandler(object sender, EventArgs e)
    {
        Console.WriteLine("Button clicked");
    }
    
    public static void Main()
    {
        Button button = new Button();
        
        button.Click += new Button.ClickHandler(ButtonHandler);
        
        button.SimulateClick();
        
        button.Click -= new Button.ClickHandler(ButtonHandler);
        
        button.TearDown();
    }
}