Introduction of the Events in c#

No.of Views883
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : CSharp , Miscellaneous
This article discusses the basics of events in C# and how to take advantage of events by implementing them in your applications.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Windows based application are message based. Application is communicating with windows and windows is communicating with application by using predefined messages. .Net wrap up the messages with ‘EVENTS’. And .Net reacting a particular message by handling events. Events are the message sent by an object to indicate the occurrence of an event. Event can also be defined as a member that enables an object to provide notification. Events provide a powerful means of inter-process communication. Events are used for communication between Objects. Communication between Objects is done by events.


Delegates are used as the means of wiring up the event when the message is received by the application.

Image Loading

Event Receiver
The event receiver may be
1. An application
2. An Object
3. A component

Event receiver get modified when something happens.

 

Event Sender
The Event sender may be


1. An assembly in an application
2. An object
3. System events like Mouse event or keyboard entry.


Event sender’s job is to raise the Event. Event sender doesn’t know any thing about , who and what the receiver is. To handle the event, there would be some method inside event receiver. This Event handler method will get executed each time when event is registered to be raised.

Here DELEGATE comes into action, because Sender has no idea who the receiver will be.

The PROCESS of hooking up the event handler is known as WIRING UP an Event.
A simple example of wiring up is CLICK EVENT

The EventHandler Delegate

This Delegate is defiend in .Net framework. It is defined in System namespace. All of the event that are defiend in .Net framework will use it.
1. EventHandler cannot return any value, so it should return Void.
2. Parameters are Object and EventArgs
3. The first parameter is Object, that raise the Event.
4. The Second parameter is EventArgs. This contains information about the Event.

Example :

Private void Button_Click(Object sender, EventArgs e)
{
}

Here first parameter is Button, this could be any button if there are multiple buttons on form. The Second parameter is EventArags. This mainly contains the property for which Button is used. 

Example 1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EventExample
{public partial class Form1 : Form
    {public Form1()
        {
            InitializeComponent();// Adding EventHandler to Click event of Buttonsbtn1.Click +=new EventHandler(Button_Click);
            btn2.Click +=new EventHandler(Button_Click);
        }// Button Click  Method , here signature of this is same as of Eventhandlerprivate void Button_Click(Object sender, EventArgs e)
        {string str;if (sender == btn1)
            {
                 str = btn1.Text;
                MessageBox.Show(str+" Clicked ");
            }if (sender == btn2)
            {
                str = btn2.Text;
                MessageBox.Show(str+ " Clicked ");
            }
        }
 
    }
}

In above code

Step 1: Create a WindowApplication

Step 2: Two butttons are dragged on the form. Name of buttons are btn1 and btn2 .

Step 3: At the Constructor part of Form , Eventhandlers are getting add in Button . += operartor is being used for adding EventHandlers.

Step 4 : Button_Click is a method having same signature as of EventHandler.

Output:

When button Scott is clicked , in message Box Scott is displaying. In this case Button_Click method is called by btn1_click delegate. In Button_Click() method , checking has been done for which Button object is calling the event. Or in other words which button is event sender. Code snippet which is checking the sender is

if (sender == btn1)
            {
                 str = btn1.Text;
                MessageBox.Show(str+" Clicked ");
            }if (sender == btn2)
            {
                str = btn2.Text;
                MessageBox.Show(str+ " Clicked ");
            }
Image Loading

Publish and Subscribe of Event

1. In Design pattern, the creator of Control (like Button, List etc) “PUBLISHES” the events to which the button will respond (Such as click).
2. Programmer who use 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
3. Mechanism of publishing is “Creating a Delegate”.
4. Mechanism of subscribing is to create a “method “with same signature as of delegate.
5. The Subscribing method is called the “Event Handler”
6. 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”.
7. 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 infoOfEventpublic 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 publishClasspublic 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 observerClasspublic 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 observerClass2public 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());
        }
    }#endregionclass 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();
        }
    }
}

 Thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top