1

I have attached a ContextMenuStrip to a Chart control.

How can I get the chart control coordinates where the top left of the ContextMenuStrip is located when it appears?

This is the point I want to translate to chart coordinates:

enter image description here

Eerik Sweden
  • 375
  • 6
  • 17

2 Answers2

0

I added a ContextMenuStrip_Opening event to the ContextMenuStrip. There I could save the menu popup location.

I did something like this:

menuPopupLocation = chart.PointToClient(System.Windows.Forms.Cursor.Position);

then I could use:

double dataX = chart.ChartAreas[0].AxisX.PixelPositionToValue(menuPopupLocation.X);
double dataY = chart.ChartAreas[0].AxisX.PixelPositionToValue(menuPopupLocation.Y);
Eerik Sweden
  • 375
  • 6
  • 17
0

Using the answer to query the current mouse cursor position in the Opening event of the ContextMenuStrip can deliver the wrong position when opening the strip is delayed a bit.

If you need the really clicked position, remove the ContextMenuStrip from the property of the control and manually open the context menu in the MouseDown event of the control. There you have access to the actual click position.

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    if(e.Button == MouseButtons.Right)
    {
        var relativeClickedPosition = e.Location;
        var screenClickedPosition = (sender as Control).PointToScreen(relativeClickedPosition);
        contextMenuStrip1.Show(screenClickedPosition);
    }
}

relativeClickedPosition is the cursor position relative to the control.

NineBerry
  • 26,306
  • 3
  • 62
  • 93