简体   繁体   中英

remove one Control from ToolTip in .net winforms

Setting tooltip on a winform control is like adding something to a dictionary:

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:

tt.SetToolTip(control, null);

tt must be holding a reference to control. 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? Or will tt hold control in its 'dictionary' with null value? In the latter case how to remove this one control for good? (I know tt.RemoveAll() but I need to keep the other tooltips.)

You can take a look at the source code for Tooltip.SetToolTip ,
look here for SetToolTipInternal
tools is a Hashtable and passing null does call 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);
        }
        ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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