如何循环浏览文本框控件?(C# WinForms)
本文关键字:控件 WinForms 浏览 何循环 循环 文本 | 更新日期: 2023-09-27 18:33:29
我有 49 个文本框,它们以类似表格的格式出现在表单上。每行文本框属于不同的类别。我想知道我将如何在数组中循环浏览它们?
我的意思是:
txtName(0)。发短信
txtOtherName(0).发短信
txtName(1).发短信
txtOtherName(1).发短信
(等等...其中 (#) 是文本框名称的唯一后缀。
如果可能的话,我试图避免将它们分配所有单独的变量(在每个组中)。所以我总共有 7 个变量和 49 个文本框。
任何帮助,不胜感激。
谢谢
您可以通过这种方式遍历每个控件
foreach (var control in this.Controls)
{
var textBox = control as TextBox;
if (textBox != null)
{
// do your stuff here
}
}
这可能会有所帮助: root
是容器或窗口,id
是控件:
#region Find Control Recursive
public Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
return root;
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
return t;
}
return null;
}
#endregion