Add TextBox.Text to a list using a for loop

后端 未结 3 1495
再見小時候
再見小時候 2021-01-23 19:25

I am trying to take the values in the textboxes, named sequentially from 0-9, and add that to a List using a for loop. I am having problems with the syntax or something.

3条回答
  •  梦谈多话
    2021-01-23 19:54

    One solution would be to declare an array and assign amtBox'es to the individual indexes in the array and then you can iterate on that array.

    var amtBoxes = new TextBox[] { amtBox0, amtBox1, .... };
    for (int i = 0; i <= amt.Count(); i++)
    {
        amt[i] = int.Parse(amtBoxes[i].Text);
    }
    

    If you end up needing to iterate on your TextBox controls in other places I would consider making the array an instance member of your object.

提交回复
热议问题