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

前端 未结 6 1903
北荒
北荒 2020-12-20 12:12

I\'m currently adding a tooltip to a label like so:

ToolTip LabelToolTip = new System.Windows.Forms.ToolTip();
LabelToolTip.SetToolTip(this.LocationLabel, te         


        
6条回答
  •  -上瘾入骨i
    2020-12-20 12:41

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

    class ToolTipHelper
    {
        private static readonly Dictionary tooltips = new Dictionary();
    
        public static ToolTip GetControlToolTip(string controlName)
        {
            
        }
    }
    

    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:

    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.Active true or false.

    edit

    Further improved on this:

    class ToolTipHelper
    {
        private static readonly Dictionary tooltips = new Dictionary();
        public static ToolTip GetControlToolTip(string controlName)
        {
            
        }
        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:

    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.

提交回复
热议问题