Click here to Skip to main content
15,895,777 members
Articles / Programming Languages / C#

Delegates And Events - The Uncensored Story - Part 1

Rate me:
Please Sign up or sign in to vote.
4.59/5 (62 votes)
19 Nov 2000 399.7K   2.2K   222  
This is a part of a series of articles that aims at fully understanding delegates and events
using System;

public delegate void ButtonEventHandler(object sender, ButtonEventArgs e);

public class ButtonEventArgs : EventArgs
{
	public string msg;
	
	public ButtonEventArgs(string message)
	{
		msg=message;
	}
}

class button
{
	private ButtonEventHandler beh=null; // For click
	  private ButtonEventHandler bep=null; // For press

	public void AddOnClick(ButtonEventHandler handler)
	{
		Console.WriteLine("Adding click event handler");
		beh=(ButtonEventHandler)Delegate.Combine(beh, handler);
	}
	
	public void RemoveOnClick(ButtonEventHandler handler)
	{
		Console.WriteLine("Removing click event handler");
		beh=(ButtonEventHandler)Delegate.Remove(beh, handler);
	}	
	
	protected virtual void OnClick(ButtonEventArgs e)
	{
		if (beh!=null)
		{
			beh(this, e);
		}
	}
	
	public void AddOnPress(ButtonEventHandler handler)
	{
		Console.WriteLine("Adding Press Event handler");
		bep=(ButtonEventHandler)Delegate.Combine(bep, handler);
	}
	
	public void RemoveOnPress(ButtonEventHandler handler)
	{
		Console.WriteLine("Removing Press Event handler");
		bep=(ButtonEventHandler)Delegate.Remove(bep, handler);
	}	
	
	protected virtual void OnPress(ButtonEventArgs e)
	{
		if (bep!=null)
		{
			bep(this, e);
		}
	}
	
	public void click()
	{
		ButtonEventArgs bea=new ButtonEventArgs("clicked");
		OnClick(bea);
	}
	
	public void press()
	{
		ButtonEventArgs bea=new ButtonEventArgs("pressed");
		OnPress(bea);
	}
}

public class DelegatesAndEvents
{
	private button button1=new button();
	private DelegatesAndEvents()
	{
		button1.AddOnClick(new ButtonEventHandler(ClickEventHandler1));
			button1.AddOnClick(new ButtonEventHandler(ClickEventHandler2));
			button1.AddOnClick(new ButtonEventHandler(ClickEventHandler3));
		
		button1.AddOnPress(new ButtonEventHandler(PressEventHandler));
 		button1.click();
		button1.press();
	}
	
	public static void Main(String [] args)
	{
		DelegatesAndEvents d=new DelegatesAndEvents();
	}
	
	public void ClickEventHandler1(object sender, ButtonEventArgs e)
	{
		Console.WriteLine("click event handler1");
		Console.WriteLine(e.msg);
		Console.WriteLine("after");
	}
	
	public void ClickEventHandler2(object sender, ButtonEventArgs e)
	{
		Console.WriteLine("click event handler2");
		Console.WriteLine(e.msg);
		Console.WriteLine("after");
	}

	public void ClickEventHandler3(object sender, ButtonEventArgs e)
	{
		Console.WriteLine("click event handler3");
		Console.WriteLine(e.msg);
		Console.WriteLine("after");
	}

	
	public void PressEventHandler(object sender, ButtonEventArgs e)
	{
		Console.WriteLine("press event handler");
		Console.WriteLine(e.msg);
		Console.WriteLine("after");
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions