You need:
foreach (TextBox element in list)
{
element.Text = "5.0";
}
In your first code sample list[i]
represents a single item in the list a TextBox
, in your foreach
loop you have element
for the same thing.
IMO, Usually, modification in foreach
loop should be avoided, you might end up writing code, where you are changing the actual list, (not just a property of an object inside the list), and end up with an exception, “Collection was modified after enumeration”.
1
solved Using a foreach loop to loop through a list of TextBoxes [closed]