10 June 2009

Customized Event and Event Handler

Every .NET developer is familiar with Event and EventHandler. ‘Click’ event of a button is the most common event. Let us take a look at their usage.

Say, we have a form containing a button named ‘button1’. Then we can add a click handler with following code inside the form.


private void button1_Click(object sender, EventArgs e)

{

Console.WriteLine("button1 has been Clicked");

}

Having a careful look at the code, we find that we have to add an event handler to button object’s Click event. The event handler takes two argument sender and event argument. The event argument usually contains information related with the event.

Now, let us come to our customized event and event handler. Say, there is a person who can change his/her phone no. We want to add an event to be fired when the the person changes his/her phone no. For this, we have to do the following tasks-

1. Define our customized event arguments PhoneNoChangedEventArgs (2 attributes- OldPhoneNo & NewPhoneNo)

2. Declare prototype of event handler (2 parameters- sender and arguments)

3. Define a class Person (2 attributes- Name & PhoneNo)

4. Declare the exact name of the event- PhoneNoChanged

5. Fire the event (if any handler is added with the event) when the event actually occurs


The code is given below following exactly the upper steps. Comments is there for better understanding.


using System;

namespace CustomizedEventHandler

{

// step 1. Define our customized event arguments PhoneNoChangedEventArgs (2 attributes- OldPhoneNo & NewPhoneNo)

public class PhoneNoChangedEventArgs : EventArgs

{

public readonly string OldPhoneNo;

public readonly string NewPhoneNo;

public PhoneNoChangedEventArgs(string oldPhoneNo, string newPhoneNo)

{

this.OldPhoneNo = oldPhoneNo;

this.NewPhoneNo = newPhoneNo;

}

}

// step 2. Declare prototype of event handler (2 parameters- sender and arguments)

public delegate void PhoneNoChangedEventHandler(object sender, PhoneNoChangedEventArgs e);

// step 3. Define a class Person (2 attributes- Name & PhoneNo)

public class Person

{

private string _name;

private string _phoneNo;

// step 4. Declare the exact name of the event- PhoneNoChanged

public event PhoneNoChangedEventHandler PhoneNoChanged;

public Person(string name, string phoneNo)

{

_name = name;

_phoneNo = phoneNo;

}

public string Name

{

get { return _name; }

}

public string PhoneNo

{

get { return _phoneNo; }

set

{

//step 5. Fire the event (if any handler is added with the event) when the event actually occurs

if (_name != value) // if phone no changed

{

string previousPhoneNo = _phoneNo; ;

_phoneNo = value;

PhoneNoChangedEventArgs evArgs = new PhoneNoChangedEventArgs(previousPhoneNo, _phoneNo);

firePhoneNoChangedEvent((object)this, evArgs);

}

}

}

private void firePhoneNoChangedEvent(object sender, PhoneNoChangedEventArgs e)

{

if (PhoneNoChanged != null) // if some hadler is added with the event

PhoneNoChanged(sender, e);

}

}

}


The sample code to test the above procedure:


static void Main()

{

// create a person object with PhoneNoChangedEventHandler

Person p = new Person("Alor Chhota", "01911515020");

p.PhoneNoChanged += new PhoneNoChangedEventHandler(p_PhoneNoChanged);

// change phone no

p.PhoneNo = "01730012311";

}

static void p_PhoneNoChanged(object o, PhoneNoChangedEventArgs e)

{

Console.WriteLine("Phone no. changed from " + e.OldPhoneNo + " to " + e.NewPhoneNo);

}

0 Comments:

 

© 2007 t!ps n tr!cks: Customized Event and Event Handler



Template unik dari rohman


---[[ Skip to top ]]---