if (sender == btn1)
{
str = btn1.Text;
MessageBox.Show(str+" Clicked ");
}
if (sender == btn2)
{
str = btn2.Text;
MessageBox.Show(str+ " Clicked ");
}
Publish and Subscribe of Event
- In Design pattern, the creator of Control (like Button, List etc) "PUBLISHES" the events to which the button will respond (Such as click).
- Programmer who uses the Button (those who put Button on their Form) may choose to "SUBSCRIBE" to one or more of the Button's event. For example As a programmer any one could choose to subscribe ( or Notify) click event of Button but not Mouse Hover over the Button
- Mechanism of publishing is "Creating a Delegate".
- Mechanism of subscribing is to create a "method "with same signature as of delegate.
- The Subscribing method is called the "Event Handler"
- In .Net Framework all event handlers return void and take two parameters. The first parameter is "Source" of the event. The Second parameter is object derived from "EventArgs".
- EventArgs is the base class for all event data. Other than its constructor, this class inherits all method from Object class. It contains a public static field named "Empty". This represents an Event with no state. The EventArgs derived class contains information about the Event.
Publish and Subscribe Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace EventExampleTime
{
#region infoOfEvent
public class TimeEvent : EventArgs
{
public readonly int Hour;
public readonly int Minute;
public readonly int Second;
public TimeEvent(int hour, int minute, int second)
{
this.Hour = hour;
this.Minute = minute;
this.Second = second;
}
}
#endregion
#region publishClass
public class Clock
{
private int hour;
private int minute;
private int second;
public delegate void SecondChangeHandlerDelegate(object clock, TimeEvent timeInformation);
public SecondChangeHandlerDelegate SecondChanged;
protected virtual void OnSecondChanged(TimeEvent e)
{
if (SecondChanged != null)
{
SecondChanged(this, e);
}
}
public void Run()
{
for (; ; )
{
Thread.Sleep(10);
DateTime dt = DateTime.Now;
if(dt.Second!= second)
{
TimeEvent timeInformation = new TimeEvent(dt.Hour, dt.Minute, dt.Second);
OnSecondChanged(timeInformation);
}
this.second = dt.Second;
this.minute = dt.Minute;
this.hour = dt.Hour;
}
}
}
#endregion
#region observerClass
public class DisplayClock
{
public void Subscribe(Clock theClock)
{
theClock.SecondChanged += new Clock.SecondChangeHandlerDelegate(timeHasChanged);
}
public void timeHasChanged(object theClock, TimeEvent ti)
{
Console.WriteLine("Current Time : {0}:{1}:{2}", ti.Hour.ToString(), ti.Minute.ToString(), ti.Second.ToString());
}
}
#endregion
#region observerClass2
public class LogCurrentTime
{
public void Subscribe(Clock theClock)
{
theClock.SecondChanged += new Clock.SecondChangeHandlerDelegate(writelogentry);
}
public void writelogentry(object theClock, TimeEvent ti)
{
Console.WriteLine(" Logging to File : {0}:{1}:{2}", ti.Hour.ToString(), ti.Minute.ToString(), ti.Second.ToString());
}
}
#endregion
class Program
{
static void Main(string[] args)
{
Clock theClock = new Clock();
DisplayClock dc = new DisplayClock();
dc.Subscribe(theClock);
LogCurrentTime lct = new LogCurrentTime();
lct.Subscribe(theClock);
theClock.Run();
Console.Read();
}
}
}