How do I remove a tooltip currently bound to a control?
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).
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.
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.