मैं यह सब स्थिर इसलिए की तरह बनाने के लिए गेविन स्टीवंस के कोड को संशोधित:
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
को सत्य या गलत सेट कर सकते हैं।
संपादित
इसके अलावा इस पर सुधार:
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");
आप डॉन हैं ' पुराने कार्यों तक पहुंच की आवश्यकता नहीं है, आप उन्हें जोड़ सकते हैं और/या उन्हें निजी बना सकते हैं, इसलिए SetToolTip()
केवल एक ही है जिसे आप कभी भी चाहते हैं ई।
स्रोत
2012-05-05 20:41:35
आह, तो मैं बस एक वर्ग उदाहरण चर के बजाय एक टूलटिप वस्तु धारण करना चाहिए था, और उसका फिर से उपयोग जब यह परिवर्तित करने की जरूरत है। बहुत धन्यवाद। – benmatth