问题描述
我有一个带有 6 个 TextBoxes
(txtNumber1
、txtNumber2
、...)的 WinForm.当我单击按钮时,我生成 6 个随机数并将它们添加到 ArrayList
,然后我想在每个 TextBox
中放入这些数字中的 1 个.>
我可以通过执行 txtNumber1.Text = arraylist[0];
等等...
我想知道是否有一种方法可以像在 JavaScript 中使用 for
循环那样执行此操作,只需更改 TextBox
id 的编号并避免将数字一一相加.
试试这个:
int i = 0;foreach(Controls.OfType()中的TextBox控件){(control).Text = arraylist[i].ToString();我++;}
这将遍历表单的所有TextBoxes
,并将它们的文本设置为ArrayList
的值.当然,TextBoxes
的数量和 ArrayList
的长度应该相同,那么这段代码应该适合你.
I have a WinForm with 6 TextBoxes
(txtNumber1
, txtNumber2
, ...). When I click the button, I generate 6 random numbers and add they to a ArrayList
, and then I want to put 1 of these numbers in each of the TextBox
.
I can do this by doing txtNumber1.Text = arraylist[0];
and so on...
I wonder if there is a way I can do this like I would do with JavaScript using a for
loop, just changing the number of the TextBox
id and avoid having to add the numbers one by one.
Try this:
int i = 0;
foreach (TextBox control in Controls.OfType<TextBox>())
{
(control).Text = arraylist[i].ToString();
i++;
}
This iterate through all TextBoxes
of the form and set their texts to ArrayList
's values. Of course the count of the TextBoxes
and the ArrayList
's length should be same then this code should works fine for you.
这篇关于从 Windows 窗体中选择所有文本框并向每个文本框添加特定文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!