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.
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);
}
}
..but txtText[i] is not allowed.
How can I loop through these boxes?
Assuming the txtText array contains references to TextBox objects you can do this
Try creating a list of textboxes instead of an Array like this:
Then use a foreach to access every item at once like this:
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.
You could use
assuming their IDs have been specified correctly, but personally I would use the collections designed for this sort of thing.
you can try like this....
I don't think you can make array objects like that anymore in the designer.
Anyway what you can do: you can make a class variable
IEnumerable<Textbox> _textboxes
, and fill it with all textboxes in the constructor.then later in your code you can just do