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

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?

Upvotes: 15

Views: 35045

Answers (6)

Pedro Farina
Pedro Farina

Reputation: 118

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 use .SetToolTip(Control, "Message that will apear when hover") When removing you just set it back to null with .SetToolTip(Control, null).

Upvotes: 6

Z.E.
Z.E.

Reputation: 1

I had the same problem . I added a tooTip component In Design form and used it. I define a text like " " in Misc section in property window of the control I wanted to have tooltip text. After that , every time I changed the test of tooltip for my component it does not appear the previous test assigned and worked well .

In the form code:

public ToolTip toolTip1;

(please note that when adding a toolTip to a form, code generator creates the above line , I changed its modifier to public since It was needed , but if you dont require it , do not change it)

In program to change the tooltip text of control :

toolTip1.SetToolTip(myControl, "The text I want to appear");

Upvotes: 0

Sebastian Grabert
Sebastian Grabert

Reputation: 29

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

Upvotes: 2

Darrel Hoffman
Darrel Hoffman

Reputation: 4636

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.

Upvotes: 4

Gavin Stevens
Gavin Stevens

Reputation: 693

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

Upvotes: 3

Vivek
Vivek

Reputation: 16508

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.

Upvotes: 20

Related Questions