Tuesday, 13 May 2014

what is the meaning of ( object sender, System.EventArgs e )


Let' assume i have 2 button click events.
btnTestA.Click += new EventHandler(btnTest_Click);
btnTestB.Click += new EventHandler(btnTest_Click);
If use click on btnTestB, it will call btnTest_Click method.
Now i will write btnTest_Click
private void btnTest_Click(object sender, EventArgs e)
{
Button test = sender as Button;
// etc ...
}
How can i find user has cliecked on btnTestB
here is the solution...
object sender will able to find that
Button test = sender as Button;
the above code will give the exact button name, there sender will be usefull
The EventArgs portion is a way to pass extra information to the event handler. In the case above, EventArgs is a base class and no real extra information is passed. However, alot of events use a derivative of EventArgs, which contain extra information.