来自N Textboes的数组中的数据使用For循环

时间:2011-02-23 14:08:46

标签: c# arrays for-loop textbox

表单上有“N”个文本框,

我想获取数组中文本框中的值。

使用“For”循环,

可以帮助我解决这个问题。

1 个答案:

答案 0 :(得分:2)

您可以通过调用this.Controls来获取表单上的所有控件,并循环显示那些将控件与TextBox进行比较,当它是TextBox时,您将值添加到您提到的数组中。

我会用这样的东西:

List<string> values = new List<string>();
foreach(Control c in this.Controls)
{
    if(c is TextBox)
    {
        /*I didnt need to cast in my intellisense, but just in case!*/
        TextBox tb = (TextBox)c;
        values.Add(tb.Text);
    }
 }
 string[] array = values.ToArray();