2009-07-22 231 views
12

我目前添加工具提示標籤,像這樣:如何刪除當前綁定到控件的工具提示?

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

當我需要改變這種提示爲標籤的文本改變,我嘗試做同樣添加一個新的提示。不幸的是,舊的工具提示仍在新的工具提示下,這真的很煩人。是否有一種方法可以刪除舊的工具提示,或者當我想更改標籤中的文本時,是否應該創建一個新標籤?

回答

15

創建ToolTip的單個實例,並在您喜歡的時候使用它來使用SetToolTip方法顯示它並使用Hide方法隱藏它。通常不需要創建多個ToolTip實例。

+2

啊,所以我應該只有一個類實例變量來存放一個ToolTip對象,並在需要修改時重新使用它。非常感謝。 – benmatth 2009-07-22 14:16:52

4
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; 
     } 
    } 
} 

用法:

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

我修改加文·史蒂文斯的代碼,使其所有靜態像這樣:

class ToolTipHelper 
{ 
    private static readonly Dictionary<string, ToolTip> tooltips = new Dictionary<string, ToolTip>(); 

    public static ToolTip GetControlToolTip(string controlName) 
    { 
     <same as above> 
    } 
} 

現在你再也不用實例化一個ToolTipHelper(因此它沒有需要構造函數),因此你現在可以像這樣從任何類訪問它:

ToolTip tt = ToolTipHelper.GetControlToolTip("button1"); 
tt.SetToolTip(button1, "This is my button1 tooltip"); 

對於任一版本也有用的是打開和關閉工具提示,您可以將tt.Active設置爲true或false。

編輯

在此進一步提高:

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

所以現在,在節目從任何地方設置一個工具提示是隻有一行:

ToolTipHelper.SetToolTip(button1, "This is my button1 tooltip"); 

如果你不」 t需要訪問舊功能,您可以將它們結合起來和/或將它們設爲私有,所以SetToolTip()是我們唯一的一個即

+0

我認爲加文史蒂文斯值得信任,但我喜歡靜態解決方案。沒有深入細節,我有一個非常複雜的形式,有100多個控件,每個表單提交時都有工具提示,所以你不可能殺死那些舊的。這種解決方案使我避免了相當多的頭痛。 – 333Matt 2013-06-18 18:49:39

+0

是的,靜態更好! – 2014-05-02 22:37:51

3

要簡單地從控制刪除提示,你可以修改類是這樣的:

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

,並使用此命令:

ToolTipHelper.SetToolTip(control, "") 
0

的工具提示對象在多個控制工作在相同時間。

創建工具提示的單個實例並將其用於添加和刪除任何控件的工具提示。

當添加您只需要使用 .SetToolTip(控制,「消息時,懸停,將apear」)當刪除 你只是.SetToolTip設置回空(控制,空)。