文本框值数组 [英] Textbox values to array
问题描述
我有几个文本框,我想遍历它们,检查它们是否包含值,如果包含,则将其放入数组中.
i've got a few Textboxes and I want to loop through them and check if they contain a value, and if they do, put it into an array.
这些文本框称为txtText1,txtText2 .... txtText12. 这是我到目前为止所得到的:
The textboxes are called txtText1, txtText2....txtText12. This is what I got so far:
for (int i = 1; i < 13; i++)
{
if(txtText[i] != String.Empty)
{
TextArray[i] = Convert.ToString(txtText[i].Text);
}
}
..但是不允许txtText [i].
..but txtText[i] is not allowed.
如何遍历这些框?
推荐答案
理想情况下,通过将它们放在一个数组中开始,而不是使用几个单独的变量.本质上,您需要一个文本框集合,对吗?因此,请使用集合类型.
Ideally, by putting them in an array to start with, instead of using several separate variables. Essentially you want a collection of textboxes, right? So use a collection type.
您可以使用
TextBox tb = (TextBox) Controls["txtText" + i];
假设他们的ID已正确指定,但就我个人而言,我将使用设计设计的集合.
assuming their IDs have been specified correctly, but personally I would use the collections designed for this sort of thing.
这篇关于文本框值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!