0
votes

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, " ");
            }
2
so what happens if you switch the order in these 2 lines toolTip.SetToolTip(pbFaseFlow, ToolTipText); toolTip.Active = true; - MethodMan
I added the winforms tag to your post. If this is wrong, please edit the post. - gunr2171
no change, I already tried that :-) - wvl_kszen
basically, I have the feeling that my event is now setting up the new tooltip, but I need to do something to trigger the tooltip to show (in this case, moving the mouse another pixel). Kind of like forcing a redraw of a paintbox with .invalidate, but now for a tooltip... I also tried to add some debug code to the tooltip_popup event. When I call settooltip in that first pixel, the popup event is never triggered. - wvl_kszen

2 Answers

1
votes

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;
                }
            }
        }
    }
}
0
votes

I had a very similar problem. I was using TrackMouseEvent() to reenable MouseHover so I didn't have to move the mouse out of the control and back in to get another hover event to trigger the tooltip.

The first time it worked normally but subsequent attempts to show a tooltip wouldn't happen until the mouse moved 1 pixel.

The workaround for me was to call Show() twice. I also needed to offset the tooltip slightly from the mouse position.

toolTip1.Show("Hi There", Parent, e.X + 5, e.Y + 5, 2000);
toolTip1.Show("Hi there", Parent, e.X + 5, e.Y + 5, 2000);