private void dateTimePicker1_ValueChanged(object sender, Series myseries, int multiplier, EventArgs e)
{
if (datelimitsset == 1) {
var dt1 = dateTimePicker1.Value;
chart1.Series.Clear();
for (int i = 0; i < multiplier; i++)
{
config();
myseries.Points.AddXY(Convert.ToString(date[i]), Convert.ToDouble(array[i]));
string[] rowi = { Convert.ToString(date[i]), Convert.ToString(array[i]) };
dataGridView1.Rows.Add(rowi);
}
}
}
这给了我错误:
'dateTimePicker1_ValueChanged'没有重载匹配委托'System.EventHandler'
我不完全了解事件处理程序,有人可以给我建议吗?
答案 0 :(得分:2)
System.EventHandler
的签名为(object sender, EventArgs e)
,因此您需要将方法签名更改为:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
或者保留当前签名,并在订阅事件时使用lambda表达式作为委托适配器:
dateTimePicker1.ValueChanged += (object sender, EventArgs e) =>
dateTimePicker1_ValueChanged(sender, [your Series variable], [your int multiplier variable], e);
当您使用lambda表达式作为委托适配器时,您实际上是在创建一个符合System.EventHandler
签名的委托(它传递了object
和EventArgs
参数),然后调用原始处理程序方法传递满足dateTimePicker1_ValueChanged
方法所需的所有参数。
答案 1 :(得分:1)
您不能像这样向事件处理程序添加任意参数,方法签名必须与事件委托匹配。 DateTimePicker
如何知道myseries
和multiplier
参数的传递内容?
答案 2 :(得分:1)
这是因为您的处理程序必须具有EventHandler
委托指定的相同签名。
也就是说,您必须删除两个中间参数:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
就将这些参数传递给函数的变通方法而言,您有几个选项......
Tag
属性中,并在处理程序中解析它们第二种选择应该足够明显......
第三个选项可能如下:
// In control initialization somewhere
dateTimePicker1.Tag = new DateTimePickerParams() { Series = myseries, Multiplier = multiplier }; // Where DateTimePickerParams is your own private class/struct defined explicitly for this purpose...
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
var ctl = sender as DateTimePicker;
var parameters = ctl.Tag as DateTimePickerParams;
var mySeries = parameters.Series;
var multiplier = parameters.Multiplier;
// Execute...
}