I have a C# application in which there are several textboxes with the same name except for a number on the end which starts at 1 and goes to 19. I was hoping to use a for loop to dynamically add values to these text boxes by using an arraylist. There will be situations where there will not be 19 items in the arrayList so some text boxes will be unfilled. Here is my sample code for what I am trying to do. Is this possible to do?
for (int count = 0; count < dogList.Count; count++)
{
regUKCNumTextBox[count+1].Text=(dogList[count].Attributes["id"].Value.ToString());
}
So you’ve got a collection of text boxes that are to be filled out top-to-bottom? Then yes, a collection of
TextBoxseems appropriate.If you stick your
TextBoxreferences in an array or aList<TextBox>— I wouldn’t use anArrayListas it’s considered deprecated in favor ofList<T>— then yes, you can do that:Then yes your logic is possible, you can also query the control by it’s name, though that would be heavier at runtime – so it’s a tradeoff. Yes, in this solution you must set up a collection to hold your text box references, but it will be more performant.