Introduction

Most examples of events and delegates in C# are more complicated and intimidating than a person new to both C# and OOP would like (VBA made it just too easy on us). While I will not explain the code, it is simple enough that what code to replace in a copy-paste is clear. I have created what I think may be one of the simplest examples of Event Handling in C#. A Metronome class creates events at a tick of 3 seconds, and a Listener class hears the metronome ticks and prints "HEARD IT" to the console every time it receives an event. This should give the novice programmer a clear idea what is necessary to generate and pass events. Plop the following code right into a class file in a blank C# project.

using System; namespace wildert { public class Metronome { public event TickHandler Tick; public EventArgs e = null ; public delegate void TickHandler(Metronome m, EventArgs e); public void Start() { while ( true ) { System.Threading.Thread.Sleep( 3000 ); if (Tick != null ) { Tick( this , e); } } } } public class Listener { public void Subscribe(Metronome m) { m.Tick += new Metronome.TickHandler(HeardIt); } private void HeardIt(Metronome m, EventArgs e) { System.Console.WriteLine( " HEARD IT" ); } } class Test { static void Main() { Metronome m = new Metronome(); Listener l = new Listener(); l.Subscribe(m); m.Start(); } } }

A slightly more complicated example is if the event has information passed with it, such as mouse coordinates for a mouse event or which key is pressed for a keypress event. To do this you need to create an appropriate class derived from the EventArgs class and then set an instance of it before raising the event. See below:

using System; namespace wildert { public class TimeOfTick : EventArgs { private DateTime TimeNow; public DateTime Time { set { TimeNow = value ; } get { return this .TimeNow; } } } public class Metronome { public event TickHandler Tick; public delegate void TickHandler(Metronome m, TimeOfTick e); public void Start() { while ( true ) { System.Threading.Thread.Sleep( 3000 ); if (Tick != null ) { TimeOfTick TOT = new TimeOfTick(); TOT.Time = DateTime.Now; Tick( this , TOT); } } } } public class Listener { public void Subscribe(Metronome m) { m.Tick += new Metronome.TickHandler(HeardIt); } private void HeardIt(Metronome m, TimeOfTick e) { System.Console.WriteLine( " HEARD IT AT {0}" ,e.Time); } } class Test { static void Main() { Metronome m = new Metronome(); Listener l = new Listener(); l.Subscribe(m); m.Start(); } } }

When you add a button to a form in C# and double click on the button in the form designer, you are taken to a method equivalent to "Heardit", but it will be appropriately named something like Button1_Click . Button1 is set up with a standard event handler ( System.EventHandler , which is discussed below in the comments) and its own events ( Click , MouseMove , etc). If you dig into the Form1.Designer.cs code, you will find the last necessary bit of code.

this.button1.click += new System.EventHandler(this.button1_Click);