简体   繁体   English

从.net winforms中的工具提示中删除一个控件

[英]remove one Control from ToolTip in .net winforms

Setting tooltip on a winform control is like adding something to a dictionary: 在winform控件上设置工具提示就像在字典中添加一些内容:

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

Setting the text of a control to null will make the application not to display tooltip on control anymore: 将控件的文本设置为null将使应用程序不再在控件上显示工具提示:

tt.SetToolTip(control, null);

tt must be holding a reference to control. tt必须持有对控制的引用。 I would like to be sure that removing (and disposing) control won't cause memory leak so I need to remove reference of control from tooltip. 我想确保删除(和处置)控件不会导致内存泄漏,因此我需要从工具提示中删除控件的引用。 Does setting to null remove the reference? 设置为null是否会删除引用? Or will tt hold control in its 'dictionary' with null value? 还是tt将控制权保留在具有空值的“字典”中? In the latter case how to remove this one control for good? 在后一种情况下,如何永久删除此控件? (I know tt.RemoveAll() but I need to keep the other tooltips.) (我知道tt.RemoveAll(),但我需要保留其他工具提示。)

You can take a look at the source code for Tooltip.SetToolTip , 您可以查看Tooltip.SetToolTip的源代码,
look here for SetToolTipInternal 在这里寻找SetToolTipInternal
tools is a Hashtable and passing null does call tools.Remove(control) : 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);
        }
        ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM