【问题标题】:remove one Control from ToolTip in .net winforms从 .net winforms 的 ToolTip 中删除一个控件
【发布时间】:2016-11-14 18:15:23
【问题描述】:

在 winform 控件上设置工具提示就像在字典中添加一些东西:

ToolTip tt = new ToolTip();
tt.SetToolTip(control, "tooltip text");

将控件的文本设置为 null 将使应用程序不再在控件上显示工具提示:

tt.SetToolTip(control, null);

tt 必须持有对控件的引用。我想确保删除(和处置)控件不会导致内存泄漏,因此我需要从工具提示中删除控件的引用。 设置为 null 是否会删除引用?或者 tt 会在其“字典”中以空值控制控制权吗?在后一种情况下,如何永久删除这个控件? (我知道 tt.RemoveAll() 但我需要保留其他工具提示。)

【问题讨论】:

  • 您好,您可以在这里找到答案:stackoverflow.com/questions/3903878/… 您不应该将值明确设置为 null。处理你能处理的任何东西,其余的由垃圾收集器完成。

标签: c# .net winforms tooltip


【解决方案1】:

你可以看一下Tooltip.SetToolTip的源码,
here for SetToolTipInternal
tools 是一个 Hashtable,传递 null 确实会调用 tools.Remove(control)

        ...
        bool exists = false;
        bool empty = false;

        if (tools.ContainsKey(control)) {
            exists = true;
        }

        if (info == null || String.IsNullOrEmpty(info.Caption)) {
            empty = true;
        }

        if (exists && empty) {
            tools.Remove(control);
        }
        ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 2016-08-02
    • 2010-10-27
    • 2011-01-12
    • 1970-01-01
    相关资源
    最近更新 更多