How do I remove a tooltip currently bound to a control?

29,595

Solution 1

Create a single instance of the ToolTip and use it whenever you like to show it using the SetToolTip method and use Hide method to hide it. Generally it is not necessary to create more than one ToolTip instance.

Solution 2

The tooltip object works in multiple Controls at the same time.

Create a single instance of the ToolTip and use it for adding and removing a ToolTip of any Control.

When adding you should simply use .SetToolTip(Control, "Message that will apear when hover") When removing you just set it back to null with .SetToolTip(Control, null).

Solution 3

I modified Gavin Stevens's code to make it all static like so:

class ToolTipHelper
{
    private static readonly Dictionary<string, ToolTip> tooltips = new Dictionary<string, ToolTip>();

    public static ToolTip GetControlToolTip(string controlName)
    {
        <same as above>
    }
}

Now you no longer have to instantiate a ToolTipHelper (hence it has no need for constructor), and thus you can now access this from any class like so:

ToolTip tt = ToolTipHelper.GetControlToolTip("button1");
tt.SetToolTip(button1, "This is my button1 tooltip");

Also useful with either version is to turn a ToolTip on and off, you can just set tt.Active true or false.

edit

Further improved on this:

class ToolTipHelper
{
    private static readonly Dictionary<string, ToolTip> tooltips = new Dictionary<string, ToolTip>();
    public static ToolTip GetControlToolTip(string controlName)
    {
        <same as above still>
    }
    public static ToolTip GetControlToolTip(Control control)
    {
        return GetControlToolTip(control.Name);
    }
    public static void SetToolTip(Control control, string text)
    {
        ToolTip tt = GetControlToolTip(control);
        tt.SetToolTip(control, text);
    }
}

So now, setting a ToolTip from anywhere in the program is just one line:

ToolTipHelper.SetToolTip(button1, "This is my button1 tooltip");

If you don't need access to the old functions, you could combine them and/or make them private, so the SetToolTip() is the only one you'd ever use.

Solution 4

public class ToolTipHelper
{
    private readonly Dictionary<string, ToolTip> tooltips;

    /// <summary>
    /// Constructor
    /// </summary>
    public ToolTipHelper()
    {
        this.tooltips = new Dictionary<string, ToolTip>();
    }

    /// <summary>
    /// Key a tooltip by its control name
    /// </summary>
    /// <param name="controlName"></param>
    /// <returns></returns>
    public ToolTip GetControlToolTip(string controlName)
    {
        if (tooltips.ContainsKey(controlName))
        {
            return tooltips[controlName];
        }
        else
        {
            ToolTip tt = new ToolTip();
            tooltips.Add(controlName, tt);
            return tt;
        }
    }
}

Usage:

var tt = toolTips.GetControlToolTip("button1");
tt.SetToolTip(button1, "This is my button1 tooltip");
tt = toolTips.GetControlToolTip("button2");
tt.SetToolTip(button2, "This is my button2 tooltip");

Solution 5

To simply remove the tooltip from the control, you could modify the class like this:

public static void SetToolTip( Control control, string text )
    {
        if ( String.IsNullOrEmpty( text ) )
        {
            if ( tooltips.ContainsKey(control.Name ) )
            {
                GetControlToolTip( control ).RemoveAll();
                tooltips.Remove( control.Name );
            }
        }
        else
        {
            ToolTip tt = GetControlToolTip( control );
            tt.SetToolTip( control, text );
        }
    }

and use this command:

ToolTipHelper.SetToolTip( control, "" )
Share:
29,595
スーパーファミコン
Author by

スーパーファミコン

Updated on July 27, 2022

Comments

  • スーパーファミコン
    スーパーファミコン almost 2 years

    I'm currently adding a tooltip to a label like so:

    ToolTip LabelToolTip = new System.Windows.Forms.ToolTip();
    LabelToolTip.SetToolTip(this.LocationLabel, text);
    

    When I need to change this tooltip as the label's text changes, I try doing the same to add a new tooltip. Unfortunately, the old tooltip remains under the new one, which is really annoying. Is there a method to remove the old tooltip, or should I just make a new label when I want to change the text in a label?