我想从代码实例化设计中的一些标签。我不想为每个文本框设置50个标签。就像团结一样,我想在设计中多次实例化某些对象。我该如何在Visual Studio中的C#中做到这一点。
例如,您必须动态创建标签对象:
int n = 4; // label counts
private void btnShow_Click(object sender, EventArgs e)
{
Label[] labels = new Label[n];
for (int i = 0; i < n; i++)
{
labels[i] = new Label();
// Here you can modify the value of the label which is at labels[i]
}
// add labels to Controls container
for (int i = 0; i < n; i++)
{
this.Controls.Add(labels[i]);
}