[英]C# Windows Form Application: How to put 5 different data in 5 different labels using a loop
I took a table_layout_panel
of 1 row by 5 columns and put 5 labels in all 5 columns.我采用了 1 行 x 5 列的
table_layout_panel
,并在所有 5 列中放置了 5 个标签。
Now I am trying to insert different data in these 5 labels.现在我试图在这 5 个标签中插入不同的数据。
For Example:-例如:-
using a for loop { print 1 to 5 }使用 for 循环 { 打印 1 到 5 }
The result should be like:-结果应该是这样的:-
label1.Text = "1";
label2.Text = "2";
label3.Text = "3";
label4.Text = "4";
label5.Text = "5";
Or suggest if there is any other way to do this.或者建议是否有其他方法可以做到这一点。 IDE: Visual studio 2019'
IDE:Visual Studio 2019'
It's as simple as this:就这么简单:
Label[] labels = new [] { label1, label2, label3, label4, label5 };
for (int i = 0; i < 5; i++)
{
labels[i].Text = (i + 1).ToString();
}
More reuseably:更可重用:
private static void SetControlText(params Control[] controls)
{
var i = 1;
foreach (var ctrl in controls)
{
ctrl.Text = (i++).ToString();
}
}
SetControlText(new [] { label1, label2, label3, label4, label5 });
or要么
Labels[] labels = new [] { label1, label2, label3, label4, label5 }
SetControlText(labels);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.