如何循环遍历 TextBox 控件? (C# 窗体)
How to loop through TextBox controls? (C# WinForms)
我在表单上有 49 个文本框,格式类似于 table。每行文本框属于不同的类别。我想知道如何在数组中循环遍历它们?
我的意思是:
txtName(0).Text
txtOtherName(0).Text
txtName(1).文本
txtOtherName(1).Text
((等等...))
其中 (#) 是文本框名称的唯一后缀。
我尽量避免为它们分配所有单独的变量(在每个组中)。所以我总共会有 7 个变量和 49 个文本框。
感谢任何帮助。
谢谢
可以这样遍历每个控件
foreach (var control in this.Controls)
{
var textBox = control as TextBox;
if (textBox != null)
{
// do your stuff here
}
}
这可能有帮助:root
是容器或 window,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
我在表单上有 49 个文本框,格式类似于 table。每行文本框属于不同的类别。我想知道如何在数组中循环遍历它们?
我的意思是:
txtName(0).Text
txtOtherName(0).Text
txtName(1).文本
txtOtherName(1).Text
((等等...)) 其中 (#) 是文本框名称的唯一后缀。
我尽量避免为它们分配所有单独的变量(在每个组中)。所以我总共会有 7 个变量和 49 个文本框。
感谢任何帮助。
谢谢
可以这样遍历每个控件
foreach (var control in this.Controls)
{
var textBox = control as TextBox;
if (textBox != null)
{
// do your stuff here
}
}
这可能有帮助:root
是容器或 window,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