2009-08-16 19 views

回答

3

除了約爾丹的答案,如果你不知道你的表單文本框的確切位置是,那麼這個擴展方法應該很有幫助。請注意,Form也從Control繼承,因此您可以通過該窗口或窗體上的任何控件調用它。

public static class ExtensionMethods 
{ 
    public static Control FindControl(this Control root, string name) 
    { 
     foreach (Control c in root.Controls) 
     { 
      // Check this control 
      if (c.Name == name) return c; 

      // Check this controls subcontrols 
      Control tmp = c.FindControl(name); 
      if (tmp != null) return tmp; 
     } 

     return null; 
    } 
} 

如果仍然是不是你不夠靈活,那麼你就可以遍歷System.Windows.Forms.Application.OpenForms

3

如果知道文本框和它的父控件的名稱,你可以這樣做:

TextBox tb = (TextBox)parent.Controls["name"]; 
2

既然你似乎在創建過程控制,把對它的引用在字典中。

TextBox txt = DynamicCreate(name); 
map[name] = txt; 
this.Controls.Add(txt); 

您所要做的就是在字典中查找它,而不是循環顯示窗體上的所有控件。

TextBox txt = map [「name」];

相關問題