Sunday, May 30, 2010

programs on events

Events


Event handling is familiar to any developer who has programmed graphical user interfaces

(GUI). When a user interacts with a GUI control (e.g., clicking a button on

a form), one or more methods are executed in response to the above event. Events

can also be generated without user interactions. Event handlers are methods in an

object that are executed in response to some events occuring in the application. To

understand the event handling model of the .NET framework, we need to unterstand

the concept of delegate. A delegate in C# allows us to pass methods of one

class to objects to other classes that can call those methods. We can pass method

m in class A, wrapped in a delegate, to class B and class B will be able to call

method m. We can pass both static and instance methods.

An event handler in C# is a delegate with a special signature, given below

public delegate void MyEventHandler(object sender,MyEventArgs e)

The first parameter (sender) in the above declaration specifies the object that fired

the event. The second parameter (e) holds data that can be used in the event handler.

The class MyEventArgs is derived from the class EventArgs. EventArgs is the

base class of more specialized classes, like MouseEventArgs, ListChangedEventArgs,

etc.

Thus an event is a notification sent by a sender object to a listener object. The listener

registers an event handler method with the sender. When the event occurs the

sender invokes event handler methods of all its registered listerners. The event handler

is registered by adding +=. The -= operator removes the handler from the event.

As an example we set up two classes A and B to see how this event handling mechanism

works in .NET framework. The delegates require that we define methods with

the exact same signature as that of the delegate declaration. Class A will provide

event handlers (methods with the same signature as that of the event declaration).

It will create the delegate objects and hook up the event handler. Class A will then

pass the delegate objects to class B. When an event occurs in class B, it will execute

the event handler method of class B.

110

111

// MyHandler.cs

using System;

// Step 1. Create delegate object

public delegate void MyHandler1(object sender,MyEventArgs e);

public delegate void MyHandler2(object sender,MyEventArgs e);

// Step 2. Create event handler methods

class A

{

public const string m_id = "Class A";

public void OnHandler1(object sender,MyEventArgs e)

{

Console.WriteLine("I am in OnHandler1 and MyEventArgs is {0}",e.m_id);

}

public void OnHandler2(object sender,MyEventArgs e)

{

Console.WriteLine("I am in OnHandler2 and MyEventArgs is {0}",e.m_id);

}

// Step 3. Create delegates, plug in the handler and register

// with the object that will fire the events

public A(B b)

{

MyHandler1 d1 = new MyHandler1(OnHandler1);

MyHandler2 d2 = new MyHandler2(OnHandler2);

b.Event1 += d1;

b.Event2 += d2;

}

} // end class A

// Step 4. Call the encapsulated method through the delegate (fires events)

class B

{

public event MyHandler1 Event1;

public event MyHandler2 Event2;

public void FireEvent1(MyEventArgs e)

{

if(Event1 != null) { Event1(this,e); }

}

public void FireEvent2(MyEventArgs e)

{

if(Event2 != null) { Event2(this,e); }

112 CHAPTER 8. EVENTS

}

} // end class B

public class MyEventArgs

{

public string m_id;

} // end class MyEventArgs

class Driver

{

public static void Main()

{

B b = new B();

A a = new A(b);

MyEventArgs e1 = new MyEventArgs();

MyEventArgs e2 = new MyEventArgs();

e1.m_id = "Event args for event 1";

e2.m_id = "Event args for event 2";

b.FireEvent1(e1);

b.FireEvent2(e2);

} // end Main

}

The next program shows GUI event handling in C#. We create two buttons each of

them fires an event.

// evthand.cs

using System;

using System.Windows.Forms;

using System.Drawing;

class MyForm : Form

{

MyForm()

{

// button1 top left corner

Button button1 = new Button();

button1.Text = "Button";

button1.Click += new EventHandler(HandleClick1);

Controls.Add(button1);

Button button2 = new Button();

button2.Location = new Point(40,40);

button2.Size = new Size(60,40);

113

button2.Text = "New Button";

button2.Click += new EventHandler(HandleClick2);

Controls.Add(button2);

}

void HandleClick1(object sender,EventArgs e)

{

MessageBox.Show("The click event fire!");

}

void HandleClick2(object sender,EventArgs e)

{

Console.WriteLine("Console click fire!");

}

public static void Main()

{

Application.Run(new MyForm());

}

}

No comments:

Post a Comment