iterate text boxes by a for loop

后端 未结 4 1819
滥情空心
滥情空心 2021-01-21 23:05

Say I have 10 text boxes and I want to put the same text into each of them. I don\'t want to write textBoxNum. Text = \"hello!\" ten times so I might write somethin

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-21 23:53

    Like this:

    foreach (Control c  in this.Controls)
    {
         if (c is TextBox)
         {
             ((TextBox)c).Text = "Hello";
         }
    }
    

    Assuming you want to set the text of all textboxes contained on the control\form, but can be modified for more specific scenarios

提交回复
热议问题