Search code examples
c#winformstooltip

Tooltip only shows after moving the mouse


I'm having some trouble to show a tooltip using the mousemove event. Basically, I want to show a tooltip when my mousepointer is over certain regions of a picturebox. I'm trying to accomplish this using the mousemove event, determining wether the pointer is on a sweet spot and (if so) setting the tooltip using settooltip.

this is my mousemove event (as I've noticed that mousemove is continuously triggered when a tooltip is shown, I check if the position really changed or not)

private void pbFaseFlow_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Location != OldPosition)
    {
        OldPosition = e.Location;

        // determine text for tooltip (gets loaded into a global string)
        DetermineText(Position);

        // show the coords and tooltip text for debugging in some textbox
        tbInfo.Text = e.X.ToString() + " " + e.Y.ToString() + " " + ToolTipText;

        // show tooltip
        if (ToolTipText != string.Empty)
        {
            toolTip.SetToolTip(pbFaseFlow, ToolTipText);
            toolTip.Active = true;
        }
        else
        {
            toolTip.Active = false;
        }
    }
}

This is working fine, except for when I move my mouse into a sweet spot for the first pixel. In my textbox, I can see that the text is being determined (say "test" f.e.), but the tooltip does not show. Only after I move my mouse 1 more pixel the tooltip is shown. This is a problem, because on sweetspots can be just 1 pixel wide, so the tooltip doesnt show when moving the mouse over it from left to right (it does show, when moving up/down..)

Even when I'm not checking for a position that has really changed, (I omit the e.location check), the tooltip does not show until I move the mouse 1 more pixel.

I noticed that if I never make the tooltip unactive, it does work, but I don't want anything to show outside the sweetspots..

What is happening here?

---------- edit --------------

Some more information :

When I change the code to this (basically always showing the tooltip, except now only with a single space when there's no information), the tooltip updates immediately over sweet spots. Disadvantage is that I now have an empty tooltip showing all the time when there's no data to show, pretty annoying.

            // show tooltip
            if (ToolTipText != string.Empty)
            {
                toolTip.Active = true;
                toolTip.SetToolTip(pbFaseFlow, ToolTipText);
            }
            else
            {
                toolTip.Active = true;
                ToolTipText = " ";
                toolTip.SetToolTip(pbFaseFlow, " ");
            }

Solution

  • Looks like you're missing toolTip.Show() method.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private Point OldPosition = new Point(0, 0);
            private Point ptSweetSpot = new Point(30, 30);
            private List<Point> sweetPointArea = new List<Point>() 
                { new Point(60, 60), new Point(60, 61), 
                  new Point(61, 60), new Point(61, 61)
                };
    
            public Form1()
            {
                InitializeComponent();
    
                pbFaseFlow.MouseMove += pbFaseFlow_MouseMove;
    
                //just to see it on pictureBox
                Bitmap myBitmap = new Bitmap(pbFaseFlow.Height, pbFaseFlow.Width);
                myBitmap.SetPixel(ptSweetSpot.X, ptSweetSpot.Y, Color.Red);
    
                foreach (Point p in sweetPointArea)
                {
                   myBitmap.SetPixel(p.X, p.Y, Color.Green);
                }
    
                pbFaseFlow.Image = myBitmap;
            }
            private void pbFaseFlow_MouseMove(object sender, MouseEventArgs e)
            {
                if (!e.Location.Equals(OldPosition))
                {
                    OldPosition = e.Location;
    
                    // show the coords and tooltip text for debugging in some textbox
                    tbInfo.Text = e.X.ToString() + " " + e.Y.ToString();
    
                    //are we inside sweet area?
                    if (sweetPointArea.Contains(e.Location))
                    {
                        toolTip.Active = true;
                        toolTip.SetToolTip(pbFaseFlow, "hello from sweet area!");
                        toolTip.Show(toolTip.GetToolTip(pbFaseFlow), pbFaseFlow, pbFaseFlow.Width / 2, pbFaseFlow.Height / 2);
                    }
                    //no? so maybe we're over sweet spot?
                    else if (e.Location.Equals(ptSweetSpot)) 
                    {
                        toolTip.Active = true;
                        toolTip.SetToolTip(pbFaseFlow, "hello from sweet point!");
                        toolTip.Show(toolTip.GetToolTip(pbFaseFlow), pbFaseFlow, pbFaseFlow.Width / 2, pbFaseFlow.Height / 2);                }
                    //no? ok, so disable tooltip
                    else
                    {
                        toolTip.Active = false;
                    }
                }
            }
        }
    }