关于 c#:如何删除当前绑定到控件的工具提示?

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

我目前正在向标签添加工具提示,如下所示:

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

当我需要在标签文本更改时更改此工具提示时,我会尝试执行相同操作以添加新的工具提示。不幸的是,旧的工具提示仍然在新的工具提示下,这真的很烦人。有没有办法删除旧的工具提示,或者当我想更改标签中的文本时应该创建一个新标签?


创建 ToolTip 的单个实例,并在您想使用 SetToolTip 方法显示它时使用它,并使用 Hide 方法隐藏它。通常不需要创建多个 ToolTip 实例。


工具提示对象同时作用于多个控件。

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


我修改了 Gavin Stevens 的代码,让它像这样静态:

1
2
3
4
5
6
7
8
9
class ToolTipHelper
{
    private static readonly Dictionary<string, ToolTip> tooltips = new Dictionary<string, ToolTip>();

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

现在您不再需要实例化 ToolTipHelper(因此它不需要构造函数),因此您现在可以从任何类访问它,如下所示:

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

对于任一版本也很有用的是打开和关闭工具提示,您只需将 tt.Active 设置为 true 或 false。

编辑

进一步改进:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
    }
}

所以现在,从程序中的任何地方设置 ToolTip 只需一行:

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

如果您不需要访问旧功能,您可以将它们组合起来和/或将它们设为私有,因此 SetToolTip() 是您唯一使用过的。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;
        }
    }
}

用法:

1
2
3
4
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");

要简单地从控件中删除工具提示,您可以像这样修改类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 );
        }
    }

并使用这个命令:

1
ToolTipHelper.SetToolTip( control,"" )


我遇到了同样的问题。我在设计表单中添加了一个tooTip组件并使用了它。我在我想要工具提示文本的控件的属性窗口的 Misc 部分中定义了一个类似""的文本。之后,每次我更改组件的工具提示测试时,都不会出现之前分配的测试并且运行良好。

在表格代码中:

1
public ToolTip toolTip1;

(请注意,在向表单添加工具提示时,代码生成器创建了上面的行,因为需要它,所以我将其修饰符更改为 public ,但如果您不需要它,请不要更改它)

在程序中更改控件的工具提示文本:

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