简体   繁体   English

C# Windows 窗体应用程序:如何使用循环将 5 个不同的数据放在 5 个不同的标签中

[英]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.

相关问题 如何在C#Windows窗体中将数据库表数据显示为带有循环的标签文本 - How to show database table data into labels text with loop in c# windows form 使用C#将具有不同ID的表中的数据分配给标签 - assign data from table with different id to labels using c# Windows窗体应用程序C#将文本传播到不同的文本框 - Windows form application c# spread text into different Textboxes vsto:使用C#在相同的解决方案中从Windows窗体应用程序迁移不同的excel工作簿 - vsto: Caling different excel workbooks from windows form application in same solutions using c# 如何在C#窗体应用程序的另一个(不同的)页面上打印列出的发票? - How to Print Listed invoices each on another (different) Page in C# windows form Application? 在Windows Store应用程序中使用C#在不同时间使用2个Dispatcher Timer - Using 2 Dispatcher Timer at different times in Windows Store Application with C# 相同的C#Windows Form应用程序从不同的打印机获得不同的打印尺寸 - Different print dimensions from different printers by same C# Windows Form Application C#Windows窗体应用程序函数循环 - C# Windows Form Application Function Loop 如何在 c# windows 表单中将组合框与 2 个不同的表绑定 - How to bind combox with 2 different tables in c# windows form 如何检查不同picBox中的图片是否相同? C#Windows窗体 - How to check if pictures in different picBoxes are the same? c# windows form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM