如何在c#中向窗体中添加标签数组
本文关键字:添加 加标签 数组 窗体 | 更新日期: 2023-09-27 18:01:48
如何将这些标签数组命名为tile到我的表单?此外,在表单中出现的实际标签的属性应该随着我更改代码中的属性而更改。有人能帮我一下吗?
using System; using System.Collections.Generic; using
System.ComponentModel; using System.Data; using System.Drawing;
using System.Linq; using System.Text; using
System.Threading.Tasks; using System.Windows.Forms;
namespace Piano_Tiles
{
public partial class Form1 : Form
{
public Label[] tile = new Label[4];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i=0;i<4;i++)
{
tile[i] = new Label();
tile[i].Height = 200;
tile[i].Width = 100;
tile[i].Left = (i % 3) * 100;
tile[i].Top = i * 200;
tile[i].BackColor = Color.Black;
tile[i].Visible = true;
}
}
}
}
现在,您应该添加控件到窗体的控件集合:
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 4; i++)
{
tile[i] = new Label();
tile[i].Height = 200;
tile[i].Width = 100;
tile[i].Left = (i % 3) * 100;
tile[i].Top = i * 200;
tile[i].BackColor = Color.Black;
tile[i].Visible = true;
Controls.Add(tile[i]);
}
}
您错过的关键特性:任何您试图添加到表单的控件,也应该添加到表单的Controls
集合,否则它将不会被表单显示
在循环末尾加上Controls.Add(tile[i]);