| C# Events IT-MIT |
C# EVENTS
EVENT
An event is a delegate type class member that is used by the object or
class to provide a notification to other objects that an event has
occurred
Event is not a function type. It is a data member
Events are used in GUI programming and in the windows forms
framework (System.Windows.Forms namespace)
It uses publisher-subscriber model
A publisher (server class) is an object that contains the definition of the
event and the delegate.
When the publishing class raises an event, all the subscribed
applications are notified (A publisher class object invokes the event
and it is notified to other objects).
A subscriber (client class) is an object that accepts the event and
provides an event handler.
The delegate in the publisher class invokes the method (event handler)
of the subscriber class.
DIFFERENCE BETWEEN DELEGATE AND EVENT
S.N DELEGATE EVENT
1. Delegate is nothing but function Event is a notification system
pointer (holds the references of
single or multiple methods)
2. Delegate declaration is placed at Event declaration must be placed
anywhere in the programming at inside the class
but usually it is placed at inside
the class definition
IT7004 C#.NET (Unit 2) Page 1
| C# Events IT-MIT |
3. It is a function member of a It is a data member of a class like
class like method, constructor, variable, constant
property
Event Handling Process
Register Event Listener
Event Event Event Listener
Source
Fire Event Event Handler
Figure. Event Handling
Components of Server Class
1. Event
2. Event Publisher (Event Source)
Components of Client Class
1. Event Listener
2. Event Handler
Event
Action of anything happened in GUI application
UI object that describe, what happened
Example
Click Event, Selection Event, Window Event,
Key Event, Mouse Event, etc, …
IT7004 C#.NET (Unit 2) Page 2
| C# Events IT-MIT |
Event Source (Event Publisher)
It is a component of server class
The generator of an event is called as event source
Example
Button, TextBox, Label, ListBox, RadioButon
ComboBox, PictureBox, CheckBox, etc,…
Event Listener
It is a component of client class
It is used to receive & handle the event
It is used to give the solution to event via Event Handler
Example
Click, TextChanged, SelectedIndexedChanged,
CheckedChanged, ValueChanged, etc,…
Event Handling
It is a component of client class
A method, which is responsible for handling the event is called as
event handling
It is used to provide the solution (application logic) to the events using
special interface methods
In c#, the event handling is done with help of delegates and events.
IT7004 C#.NET (Unit 2) Page 3
| C# Events IT-MIT |
PUBLISHER-SUBSCRIBER MODEL
User Control (Publisher)
Delegate declaration
Event declaration based on the delegate
Fire the Event
Notify
Subscribed events
Application 1 (Subscriber) Application 2 (Subscriber)
Subscribed Event Subscribed Event
Fires the Event Fires the Event
Handler Handler
Event Handler Event Handler
event keyword
The event keyword is used to create an instance of an event, that can
store methods in its invocation list.
SYMBOLS USED IN EVENTS
Event supports two symbols namely + and -
+ is used for adding a delegate object to event member
- is used for removing a delegate object from existing event
member
IT7004 C#.NET (Unit 2) Page 4
| C# Events IT-MIT |
NOTE
Event is a one of the data member (e.g. variable) in a class
It must be declared inside a class like instance variables
It is not possible to declare an event in a block of executable code
It is important to note that, an event member is automatically initialized
to null (by default)
Syntax
<modifier> event <delegate-name> <event-name>;
Example
// delegate declaration
delegate void MyHandler();
class Program
{
// event declaration
public event MyHandler et;
}
Importance of Delegates in Events
Instead of giving direct response to publisher class (server class),
event will provide the response to server class via delegate object.
IT7004 C#.NET (Unit 2) Page 5
| C# Events IT-MIT |
I. CREATION OF SIMPLE EVENT USING EVENTS AND DELEGATE
AIM
The aim of this code is used to create EVENT (manual event) using
event and delegate concept
event
Here it is used to publish an event (publisher pattern) and call the
event
Event response is done via delegate instance
delegate
Here it is used to give the response for event action (subscriber
pattern)
1. SOURCE CODE
using System;
using k = System.Console;
namespace EventsApp Event declaration
{
// delegate declaration
delegate void MyHandler(string s);
class EventTest
{
public event MyHandler et=null;
// server role: EVENT CREATION
public void createEvent()
{
Server Role
if (et != null)
{
// calling delegate via event, if event is not null
et("Load event fired !!!");
IT7004 C#.NET (Unit 2) Page 6
| C# Events IT-MIT |
}
else
{
RedColor();
k.WriteLine("Failure:");
WhiteColor();
k.WriteLine("\tNo event is fired. Because an event is NULL...\n");
}
}
// client role: give solution, when an event has occurred
public void LoadResponse(string msg)
{
GreenColor();
k.WriteLine("Success:\n"); Client Role
k.WriteLine("\t"+msg);
WhiteColor();
k.WriteLine("\tWelcome to C# event handling...\n");
}
static void Main()
{
// Object creation
EventTest obj = new EventTest();
obj.WhiteColor();
k.WriteLine("------------------------------------------------------------");
k.WriteLine("\tC# Events (Manual event creation)");
k.WriteLine("------------------------------------------------------------");
// call method before adding event
obj.createEvent();
IT7004 C#.NET (Unit 2) Page 7
| C# Events IT-MIT |
// delegate object creation and add a method, which is responsible
for event handler
MyHandler dg = new MyHandler(obj.LoadResponse);
// add delegate instance to event variable for client response
obj.et += dg;
// call method after adding event
obj.createEvent();
obj.defaultColor();
k.WriteLine("------------------------------------------------------------");
}
// colors method
public void RedColor()
{
k.ForegroundColor = ConsoleColor.Red;
}
public void GreenColor()
{
k.ForegroundColor = ConsoleColor.Green;
}
public void WhiteColor()
{
k.ForegroundColor = ConsoleColor.White;
}
public void defaultColor()
{
k.ResetColor();
}
}
}
IT7004 C#.NET (Unit 2) Page 8
| C# Events IT-MIT |
2. OUTPUT
IT7004 C#.NET (Unit 2) Page 9