Event Args.
could someone break this event handler down for me....what is the purpose of the 'object sender' and 'System.EventArgs e'
could someone break this event handler down for me....what is the purpose of the 'object sender' and 'System.EventArgs e'
- Ray
This event occurs when the state of a RadioButton changes. sender will contain the instance of RadioButton on which the event occured. EventArgs is the base class for all classes which contains data regarding an event.
In this case there is no data in the EventArgs, in other situations such as the KeyPress event of a Form, a class derived from EventArgs is used which would have data such as the key pressed.
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
}
However, to maintain a consistency in the event handling mechanism this signature is followed for all events even when EventArgs has no data.
As for the other part - the use of the sender object. This would be useful if you used the same method to handle multiple events. For example:
You can register this same EventHandler method for the CheckChanged event of a RadioButton and a CheckBox. In that case, you can utilize the sender instance to know which control the event fired on. In Visual Studio, a separate EventHandler is added for each control and so you are sure that only one control is using the EventHandler and so you rarely check what the sender is.
Regards,
Vikram