ToolTip for picturebox and image map

Saeid Yazdani picture Saeid Yazdani · Aug 16, 2012 · Viewed 7.9k times · Source

I have a PictureBox that has a map assigned to it (basically a list of Rectangle objects). Now I want to make it such if user moves the mouse over this picturebox, and if the mouse is over a rectangle that is existing in the list, a tooltip pop up and show some information.

The mapping works fine, but problem is the ToolTip is going crazy and does not show up properly, it just continues poping up with any mouse move. How can I code it such it only pop up when the mouse is in the rectangle?

This is my code and down there you see the example!

private void pictureBoxPackageView_MouseMove(object sender, MouseEventArgs e)
{
    if (PackageMap == null || PackageMap.Count == 0) return;
    var point = new Point(e.X, e.Y);

    foreach (var map in PackageMap)
    {
        if (map.Rectangle.Contains(point))
        {
            var tip = new ToolTip();
            tip.ToolTipTitle = map.Pin.Group;
            tip.Show("Result: " + map.Pin.Mk2Result, pictureBoxPackageView, point, 200);
            break;
        }
    }
}

enter image description here

Answer

jfs picture jfs · Aug 16, 2012

It continuously pops up because you are only checking if the cursor is inside a rectangle. Remember that you can still move the mouse several times but your still within that rectangle.

I think what you want is to pop up the tooltip only once when you enter a rectangle. You should remember the previous location of the mouse. If the current location is within a rectangle check if the previous location is not in the same rectangle. This is the time to display the tooltip otherwise pass.