C# 如何删除当前绑定到控件的工具提示?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1165540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:46:56  来源:igfitidea点击:

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

c#.netwinformstooltip

提问by スーパーファミコン

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?

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

采纳答案by Vivek

Create a single instance of the ToolTipand use it whenever you like to show it using the SetToolTipmethod and use Hidemethod to hide it. Generally it is not necessary to create more than one ToolTipinstance.

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

回答by Gavin Stevens

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

回答by Darrel Hoffman

I modified Gavin Stevens's code to make it all static like so:

我修改了 Gavin Stevens 的代码,使其全部静态,如下所示:

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:

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

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.Activetrue or false.

对任一版本也很有用的是打开和关闭工具提示,您可以只设置tt.Activetrue 或 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:

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

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.

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

回答by Sebastian Grabert

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

回答by Pedro Farina

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

创建工具提示的单个实例并使用它来添加和删除任何控件的工具提示。

添加时,您应该简单地使用 .SetToolTip(控制,“悬停时将出现的消息”)删除时,您只需使用.SetToolTip(控制,空)将其设置回空。

回答by Z.E.

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 .

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

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)

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

In program to change the tooltip text of control :

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

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