When users perform any types of activities like
Mouse over, Mouse clicks, Keyboard presses etc that means events is generated.
Events are members of class, when something happening to provide notifications to clients of that class and raise event and have the message about the events and send to the application and application is responsible and handle this event using event handlers.
Event handler is a method that has the same signature as the event and this method is executed when the event occurs.
Event keyword is used to create an instance of an event that can store methods in its invocations.
Event is declared using delegates.
If you define an event you need first to define a delegate that contains the methods that will be called when the event raised, and then you define the event based on that delegate.
Events are members of class, when something happening to provide notifications to clients of that class and raise event and have the message about the events and send to the application and application is responsible and handle this event using event handlers.
Event handler is a method that has the same signature as the event and this method is executed when the event occurs.
Event keyword is used to create an instance of an event that can store methods in its invocations.
Event is declared using delegates.
If you define an event you need first to define a delegate that contains the methods that will be called when the event raised, and then you define the event based on that delegate.
This
is done using to the += and -= operators.
when you want to register an event handler with an event you click on a button (button1_Click) i.e.
this.button1.click += new
System.EventHandler(this.button1_Click);
Example for Events as given below.
Example for Events as given below.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
ConsoleApplication2events
{
public delegate void EventHandler();
public class Program
{
public static event EventHandler _CallEvent;
static void Main()
{
// Add event handlers to Show
event.
_CallEvent += new EventHandler(Mouse);
_CallEvent += new EventHandler(Button);
// Invoke the event.
_CallEvent.Invoke();
Console.Read();
}
static void Mouse()
{
Console.WriteLine("Call to
Mouse");
}
static void Button()
{
Console.WriteLine("Call to
button");
}
}
}
The
Output as given below
Call
to Mouse
Call
to button
===========================================
Another
Example for Events
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace EventHandlers
{
public class HandlerClass
{
public delegate void TickHandler(HandlerClass m, EventArgs e);
public event TickHandler Ticks;
public EventArgs e = null;
public void Start()
{
while (true)
{
Thread.Sleep(5000);
if (Ticks != null)
{
Ticks(this, e);
}
}
}
}
public class callToHandler
{
public void TickEvent(HandlerClass m)
{
m.Ticks += new HandlerClass.TickHandler(ShowMsg);
}
private void ShowMsg(HandlerClass m, EventArgs e)
{
Console.WriteLine("This is show
msg.");
}
}
public class TestClass
{
static void Main()
{
HandlerClass Handler = new HandlerClass();
callToHandler callHandler = new callToHandler();
callHandler.TickEvent(Handler);
Handler.Start();
}
}
}
The
Output as given below
This
is show msg. (This msg call after 5 second b/c
here sleep commend is call for 5 second).