C# EventHandler with Arguments
All the methods that are called by events require two arguments:
object sender
EventArgs e
The event calls a method using those 2 parameters, so we can’t directly add a custom argument.
If we have this code and we want to display the string ‘s’ in a MessageBox…
1
2
3
4
5
6
7
8
9
10
11
private void Form1_Load (object sender, EventArgs e)
{
string s = "Hello!";
button.Click += new EventHandler(show_msg); // our string is not included in the call
}
private void show_msg(object sender, EventArgs e, string s)
// this gives an error, because the function is called with only 2 arguments, but it expects 3
{
MessageBox.Show(s);
}
Clearly this will not work because we are limited to those two parameters.
The Solution
The easiest solution is to use a delegate to call our method.
We get the shortest code using the lambda operator, whose symbol is =>.
- It is recommended for beginners because of its simplicity.
1
2
3
4
5
6
7
8
9
10
private void Form1_Load (object sender, EventArgs e)
{
string s = "Hello!";
button.Click += (sender2, e2) => show_msg(sender2, e2, s);
}
private void show_msg (object sender, EventArgs e, string s)
{
MessageBox.Show(s);
}
Without using the lambda operator, it can be rewritten using a delegate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private void Form1_Load (object sender, EventArgs e)
{
string s = "Hello!";
button.Click += delegate(object sender2, EventArgs e2)
{
show_msg(sender2, e2, s);
};
}
private void show_msg(object sender, EventArgs e, string s)
{
MessageBox.Show(s);
}
That’s all :).