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?
Create a single instance of the
ToolTip
and use it whenever you like to show it using theSetToolTip
method and useHide
method to hide it. Generally it is not necessary to create more than oneToolTip
instance.The tooltip object works in multiple Controls at the same time.
Usage:
To simply remove the tooltip from the control, you could modify the class like this:
and use this command:
I modified Gavin Stevens's code to make it all static like so:
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:
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:
So now, setting a ToolTip from anywhere in the program is just one line:
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.