我有一個動態創建(運行時創建)文本框,其名稱是可用的字符串。我想要做的就是訪問這個文本框,就像我們作爲普通文本框一樣。可以任何一個告訴我如何將其轉換爲文本框或任何其他解決方案如何訪問C#中動態創建的文本框,其名稱在字符串中可用?
0
A
回答
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」];
相關問題
- 1. 在iPhone中訪問動態創建文本字段的文本?
- 2. 創建動態字符串名稱,並設置在C#
- 3. 如何在Android中使用動態字符串名稱訪問字符串(與JavaScript中的eval類似)?
- 4. 具有動態名稱的C#ASP.NET訪問文本框?
- 5. C#創建一個名稱爲動態字符串的類
- 6. 由其名稱動態地創建一個對象(字符串)
- 7. c#mvc從字符串名稱動態創建控制器 - eval?
- 8. C#如何創建對象名稱可變字符串
- 9. 如何在JavaScript中使用其名稱的字符串來訪問類?
- 10. 如何刪除在c中創建的動態文本框#
- 11. 動態創建文本框名稱和位置問題
- 12. 如何在C#中使用動態名稱創建變量?
- 13. 訪問MVC中動態創建的文本框的POST值3
- 14. 我如何訪問一個動態創建的文本框?
- 15. 如何在C#中動態創建連接字符串
- 16. 如何在jsp頁面中訪問動態創建的文本框
- 17. 如何從字符串動態調用文本框C#WPF?
- 18. 如何通過C#中的字符串訪問EventHandler中的表單文本框?
- 19. 訪問在飛行中創建的C#中的文本框
- 20. 如何使用字符串中的名稱創建實例
- 21. 如何用字符串中的名稱創建屬性?
- 22. 在JOptionPane創建的其他方法中訪問字符串?
- 23. 從C中的文件創建動態分配的字符串
- 24. Adobe Animate:通過動態創建名稱來訪問文本字段
- 25. 如何從其「字符串名稱」訪問實例變量?
- 26. 我如何可以訪問到的UINavigationController與字符串名稱
- 27. 在C#Visual Studio中調用動態創建的文本框
- 28. 動態C字符串訪問
- 29. 在Coldfusion中創建動態文本框
- 30. 如何將動態複選框的名稱存儲在字符串數組中