PDA

Click to See Complete Forum and Search --> : EventArgs - How do I make use of the e.Equals() method?


The Thing
Dec 20th, 2006, 04:22 PM
Hi folks,

I have several buttons on my form that will be calling the same method, but with different actions being performed inside this method depending on whichever button was clicked.

What I am doing is passing into this method the EventArgs e object of each button and then trying to check this via e.Equals() to determine my course of action within the method, but I've had no luck so far. How should I go about getting this to work? I've tried the following, but it wont compile.


if(e.Equals(button1_Click()))
{
//Won't work. :(
}

if (sender.Equals(button4_Click()))
{
//I've tried passing in the sender object, but this doesn't work either.
}


I searched the forums and did a google before posting - I saw mention of delegates and events with examples - but I'm not really any the wiser for it.
A pointer in the right direction would be appreciated thanks.

jmcilhinney
Dec 20th, 2006, 05:08 PM
You're going about this the wrong way. The 'e' argument contains data for the event itself. If the type of the 'e' argument is EventArgs then it contains nothing useful. EventArgs is the base class for all other event handler arguments and is used explicitly when the event doesn't require any data. The 'sender' argument IS the object that raised the event, so in your case the 'sender' argument will be a reference to the Button that was clicked.if (sender == this.button4)
{
// button4 was clicked.
}Having said that, what's the point of having a common event handler if you want to do something different for each Button? You would use a common event handler if you have common functionality. If you want to do something different for each Button then they should have individual event handlers. Remember that if you have SOME common functionality then you can put that into a method which you can then call from the Click event handler for each individual Button.

popskie
Dec 20th, 2006, 10:45 PM
Maybe you can cast the sender to button

if (((Button)sender).Text == "btton1")
MessageBox.Show("Message");