Can't get `MouseEventArgs` in `MouseHover` event

tmwoods picture tmwoods · Jul 5, 2013 · Viewed 7.8k times · Source

I'm sure this has a straight forward answer but I can't seem to figure it out.

I am trying to add a tooltip using my mousehover event. Historically I have used the mousemove event but unfortunately this means the tooltip gets updated as fast as the program can do it. I just want it to show when the mouse is stationary on my graph.

The issue is that I can't get the e.Location property because the event handler only uses EventArgs, not MouseEventArgs. Is there a way I can change this? Or maybe add a line like MouseEventArgs mouse = new MouseEventArgs(); (I get an error saying it needs more arguments, but I don't know which).

Any help is appreciated :)

        private void chSysData_MouseHover(object sender, EventArgs e)
        {
            //Add tooltip
            try
            {
                int cursorX = Convert.ToInt32(chSysData.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X));
                tipInfo = "System: " + systemVoltage[cursorX].ToString("0.00") + Environment.NewLine + "Current: " + currArray[cursorX].ToString("0.00") + Environment.NewLine;
                tooltip.SetToolTip(chSysData, tipInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

Answer

Hans Passant picture Hans Passant · Jul 5, 2013

Map the Cursor.Position property to your chart. An example with a panel:

    private void panel1_MouseHover(object sender, EventArgs e) {
        var pos = panel1.PointToClient(Cursor.Position);
        toolTip1.Show("Hello", panel1, pos);
    }

Note that this is not otherwise different from using toolTip1.Show("Hello", panel1); but you are likely to want to tweak the tooltip position so it isn't overlapped by the cursor.