Frage

I want to zoom a chart

 private void toolStripButtonZoom_Click(object sender, System.EventArgs e)
{
    double posXStart = chartMain.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X) - (xMax - xMin) / 0.5;
    double posXFinish = chartMain.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X) + (xMax - xMin) / 0.5;
    double posYStart = chartMain.ChartAreas[0].AxisY.PixelPositionToValue(e.Location.Y) - (yMax - yMin) / 0.5;
    double posYFinish = chartMain.ChartAreas[0].AxisY.PixelPositionToValue(e.Location.Y) + (yMax - yMin) / 0.5;

    chartMain.ChartAreas[0].AxisX.ScaleView.Zoom(posXStart, posXFinish);
    chartMain.ChartAreas[0].AxisY.ScaleView.Zoom(posYStart, posYFinish);
}

it does not recognize "Location" and gives this error.

War es hilfreich?

Lösung

That error message is correct. The EventArgs class, an instance of which you're accessing through the e parameter, does not contain a Location property.

Unfortunately, that's all you get with the Click event. You need to switch to handling the MouseClick event instead, which passes a MouseEventArgs object with a Location property. This is rather simple to do, you only need to update the name of the handler method and the code that attaches your handler to the event (possibly located in the designer-generated code-behind file).

Alternatively, you can retrieve the current location of the mouse pointer using the Cursor.Current property. This is often "good enough", but do keep in mind a couple of things:

  1. The Click event is not only raised in response to mouse events, but also in certain other cases, like when the control is focused and the user presses the Enter key. In those cases, the current location of the mouse pointer may be completely meaningless.

    That's why the MouseClick event is a better option. Not only does it give you the location information for free, but it is only raised in response to mouse events, when the Location property will be meaningful.

  2. The user could have moved the mouse between the time that the Click event was generated and the time that your event handler executes, which means that Cursor.Current returns a different location than where the user originally clicked. In most cases this is not a significant distance, but it could be.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top