在 C# 中将数组中的标签置于前面
Bring to front labels in array in c#
我创建了一组标签,我试图将这些标签显示在包含游戏板图片的图片框上。我一直在图片框后面显示标签,但不确定自己做错了什么。
public Form1()
{
InitializeComponent();
int x = 100;
int y = 0;
// create 361 labels, set their dimensions
for (int i = 0; i < 361; i++)
{
board[i] = new Label();
board[i].Parent = pictureBox1;
board[i].Location = new Point(x, y);
board[i].Name = "label" + i;
board[i].Width = 55;
board[i].Height = 55;
board[i].Text = "0";
board[i].BackColor = Color.Black;
board[i].BringToFront();
}
// set the position of the label
foreach (Label i in board)
{
if (x >= 580)
{
x = 0;
y = y + i.Height + 55;
}
i.Location = new Point(x, y);
this.Controls.Add(i);
x += i.Width;
}
将标签添加到表单容器后将调用移至 BringToFront
// set the position of the label
foreach (Label i in board)
{
if (x >= 580)
{
x = 0;
y = y + i.Height + 55;
}
i.Location = new Point(x, y);
this.Controls.Add(i);
i.BringToFront();
x += i.Width;
}
顺便说一句,收获不大,但是您可以将此代码放在第一个循环中并删除 foreach 循环
我创建了一组标签,我试图将这些标签显示在包含游戏板图片的图片框上。我一直在图片框后面显示标签,但不确定自己做错了什么。
public Form1()
{
InitializeComponent();
int x = 100;
int y = 0;
// create 361 labels, set their dimensions
for (int i = 0; i < 361; i++)
{
board[i] = new Label();
board[i].Parent = pictureBox1;
board[i].Location = new Point(x, y);
board[i].Name = "label" + i;
board[i].Width = 55;
board[i].Height = 55;
board[i].Text = "0";
board[i].BackColor = Color.Black;
board[i].BringToFront();
}
// set the position of the label
foreach (Label i in board)
{
if (x >= 580)
{
x = 0;
y = y + i.Height + 55;
}
i.Location = new Point(x, y);
this.Controls.Add(i);
x += i.Width;
}
将标签添加到表单容器后将调用移至 BringToFront
// set the position of the label
foreach (Label i in board)
{
if (x >= 580)
{
x = 0;
y = y + i.Height + 55;
}
i.Location = new Point(x, y);
this.Controls.Add(i);
i.BringToFront();
x += i.Width;
}
顺便说一句,收获不大,但是您可以将此代码放在第一个循环中并删除 foreach 循环