iterate text boxes by a for loop

后端 未结 4 1822
滥情空心
滥情空心 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:58

    You either need to load all of your textboxes into a list or array structure, and this will allow you to iterate over it.

    TextBox[] boxes = { tb1, tb2, tb3, ... };
    

    Otherwise, you could inspect the Controls property of your form/container for items of the TextBox type. If the controls could be nested in deeper containers, you might need to recursively explore them (at this point, I would seriously consider an array approach, unless you have some ghastly number of textboxes to load). But as a starting point, you might have

    foreach (var tb in this.Controls.OfType())
    {
        tb.Text = "whatever";
    }
    

提交回复
热议问题