简体   繁体   English

如何在C#中读取多个文本框

[英]how read multiple text box in C#

i have about twenty text box in visual C# which their name are text box1,textbox2 and.... . 我在可视C#中有大约二十个文本框,它们的名称是text box1,textbox2和...。

User inserts number in each text box and i want to read these numbers and save them in array like a[]. 用户在每个文本框中插入数字,我想读取这些数字并将其像a []一样保存在数组中。

I tried this code but it took long time to do for all array component 我尝试了这段代码,但是花了很长时间来处理所有数组组件

a[0]=Convert.ToInt32(textBox1.Text);

Is there any way which i use "for" loop that computer read automatically read text box and save the number in a[] array in order? 我有什么方法可以使用“ for”循环让计算机自动读取文本框并按顺序将数字保存在[[]数组中?

thank you 谢谢

Yes, use the Controls collection . 是的,请使用Controls集合 Here is a simple example: 这是一个简单的示例:

for (int i=0; i<= a.GetUpperBound(0); i++)
{
    var name = string.Format("textBox{0}", i+1);
    var textbox = this.Controls[name] as TextBox;
    a[i] = textbox.Text;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM