1
votes

I want it to move when the mouse moves, and disappear when the pointer isn't over the label.

This doesn't work:

private void lblRevisionQuestion_MouseMove(object sender, MouseEventArgs e)
{
    toolTip1.Show("test", this, PointToClient(MousePosition), Int32.MaxValue);
}

private void lblRevisionQuestion_MouseLeave(object sender, EventArgs e)
{
    toolTip1.Hide(this);
}

As soon as the tooltip appears, it steals focus away from the form, evoking MouseLeave. Then the tooltip hides, and the pointer is once again over the label, invoking MouseMove. This results in a choppy, flashing tooltip.

Is there any way to do this?

3
With the given code, the tooltip window displays exactly at the mouse position. That causes the flicker. Offset it a bit. - Hans Passant

3 Answers

0
votes
toolTip1.Show(_toolTipText, this, new Point(lblRevisionQuestion.Left + e.X + 1, lblRevisionQuestion.Top + e.Y + 1), int.MaxValue);

Oddly enough, when I tried displaying it to some arbitrary coordinates eariler, it had the same problem as above. I don't know why this works and that didn't.

0
votes

Since your are working with a list view, I would like to bring to your notice that the listview items have some tooltip specific properties like ToolTipText. This will make it easier to display the data when you hover over a item as shown below

toolTip1.ToolTipTitle = string.Format("Item: {0}",e.Item.Text);
toolTip1.Show(e.Item.ToolTipText,listView1);
toolTip1.ShowAlways = true;
0
votes

This is how I did:

MyToolTip.cs :

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

public class MyToolTip : ToolTip
{
    public MyToolTip()
    {
        OwnerDraw = true;
        Draw += MyToolTip_Draw;
    }

    private void MyToolTip_Draw(object sender, DrawToolTipEventArgs e)
    {
        using (StringFormat sf = new StringFormat())
        {
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.None;
            sf.FormatFlags = StringFormatFlags.NoWrap;
            using (Font f = new Font("Arial", 7.5F))
            {
                SizeF s = new SizeF();
                s = e.Graphics.MeasureString(e.ToolTipText, f);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(225, 225, 245)), e.Bounds);
                e.DrawBorder();
                e.Graphics.DrawString(e.ToolTipText, f, SystemBrushes.ActiveCaptionText, e.Bounds, sf);
            }
        }
    }

}

Using it somewhere in a class:

private MyToolTip ttp;
private int LastX;
private int LastY;
public string Caption { get; set; }

private void MyObjectWhichNeedsMovingToolTip_MouseLeave(object sender, EventArgs e)
{
    ttp.Hide(this);
}

private void MyObjectWhichNeedsMovingToolTip_MouseMove(object sender, MouseEventArgs e)
{
    // This is for stop flickering tooltip
    if (e.X != this.lastX || e.Y != this.lastY)
    {
        // depending on parent of the object you must add or substract Left and Top information in next line
        ttp.Show(Caption, this, new Point(MyObjectWhichNeedsMovingToolTip.Left + e.X + 10, MyObjectWhichNeedsMovingToolTip.Top + e.Y + 20), int.MaxValue); 

        this.lastX = e.X;
        this.lastY = e.Y;
    }
}

And the result is:

FloatingToolTip