How do I remove a tooltip currently bound to a con

2019-01-27 13:46发布

问题:

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?

回答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.



回答2:

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.



回答3:

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");


回答4:

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, "" )


回答5:

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).