6

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);
            }
        }
tmwoods
  • 2,353
  • 7
  • 28
  • 55

1 Answers1

11

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I'm reading up on this error: `Member 'System.Windows.Forms.Cursor.Position.get' cannot be accessed with an instance reference; qualify it with a type name instead`. I've never seen it before. Any suggestions while I try to figure it out? – tmwoods Jul 05 '13 at 18:14
  • "Cursor" is the type name, not the name of an object. Make sure that doesn't clash with a variable you use in your code, you are using names like "cursorX". If that doesn't help then click the Ask Question button to better document your problem. – Hans Passant Jul 05 '13 at 18:16
  • I just searched my program and no variable named `Cursor`. I'll keep digging around first before I ask another question. I don't like to ask unless I have researched it for myself first. Thanks for the help :) – tmwoods Jul 05 '13 at 18:23
  • Figured it out: ` var pos = chSysData.PointToClient(System.Windows.Forms.Cursor.Position);`. Thanks again! – tmwoods Jul 05 '13 at 18:27