Search code examples
c#.netwinformsmschartmousehover

C# MSChart add hover Line


When you click into a mschart a red line appears, is it somehow possible to add this line while hovering over the chart? I would like to make this line visible while hovering and add the result of the hovered datapoint under the line or something like that. So far I only found out about tooltip and was able to use it and show values of datapoint while hovering over it.
If you dont know about what line I'm talking:
enter image description here


Solution

  • You can handle MouseHover event of the chart and get the data point and then set the cursor position SetCursorPosition:

    private void chart1_MouseHover(object sender, EventArgs e)
    {
        var p = chart1.PointToClient(MousePosition);
        chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(p, true);
    }
    

    You also need to handle MouseMove and call protected ResetMouseEventArgs method of the control to raise MouseHover as expected:

    private void chart1_MouseMove(object sender, MouseEventArgs e)
    {
        var ResetMouseEventArgs= 
            chart1.GetType().GetMethod("ResetMouseEventArgs",
            System.Reflection.BindingFlags.NonPublic |
            System.Reflection.BindingFlags.Instance);
        ResetMouseEventArgs.Invoke(chart1, null);
    }