简体   繁体   中英

No overload for 'dateTimePicker1_ValueChanged' matches delegate 'System.EventHandler'

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);
    }
  }
}

This is giving me the error:

No overload for 'dateTimePicker1_ValueChanged' matches delegate 'System.EventHandler'

I do not fully understand event handlers, can anyone give me advice?

The signature for System.EventHandler is (object sender, EventArgs e) so you either need to change your method signature to this:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)

Or keep your current signature and use a lambda expression as a delegate adapter when you subscribe to the event:

dateTimePicker1.ValueChanged += (object sender, EventArgs e) => 
 dateTimePicker1_ValueChanged(sender, [your Series variable], [your int multiplier variable], e);

When you use a lambda expression as a delegate adapter, you are essentially creating a delegate which conforms to the System.EventHandler signature (it is passed an object and an EventArgs argument), which then calls your original handler method passing all of the arguments required to satisfy your dateTimePicker1_ValueChanged method.

The reference documentation for the System.EventHandler delegate .

EDIT: documentation for an example handler for the DateTimePicker.ValueChanged event

You can't add arbitrary parameters to the event handler like that, the method signature must match the event delegate. How would the DateTimePicker know what to pass for the myseries and multiplier parameters?

It's because your handler must have the same signature specified by the EventHandler delegate.

That is, you'll have to remove your two middle parameters:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}

In terms of a workaround for passing these parameters into the function, you have a few options...

  1. Generate the event handler as an anonymous function (as per James' answer )
  2. Store and retrieve them from instance variables
  3. Store them on the DateTimePicker control's Tag property and resolve them in the handler

The second option should be obvious enough...

The third option might look like:

// 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...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM