通过循环遍历文本框 [英] iterate text boxes by a for loop
问题描述
说我有10个文本框,我希望把相同的文本到每个人。我不想写 textBoxNum。文本=你好!
所以十倍我可能会这样写:
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 something like this:
for(int i=1; i<=10; i++)
{
textBox + i. Text = "hello!";
}
显然,这是行不通的。
Obviously, it doesn't work.
怎么可以这样用为
循环中完成?
How can this be done with a for
loop ?
推荐答案
您要么需要将所有的文本框加载到列表或阵列结构,这将让你遍历它。
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<TextBox>())
{
tb.Text = "whatever";
}
这篇关于通过循环遍历文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!