简体   繁体   English

在C#中带到数组中的前标签

[英]Bring to front labels in array in c#

I created an array of labels that I am trying to display on a picturebox that has a picture of a game board. 我创建了一个标签数组,试图在带有游戏板图片的图片框上显示这些标签。 I keep displaying the labels behind the picturebox and am not sure what I'm doing wrong. 我一直在图片框后面显示标签,但不确定我做错了什么。

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;
        }

Move the call to BringToFront AFTER having added the labels to the forms container 将标签添加到表单容器后,将调用移至BringToFront AFTER

// 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;
}

By the way, not a big gain, but you could put this code inside the first loop and remove the foreach loop 顺便说一下,这不是什么大的收获,但是您可以将这段代码放在第一个循环中并删除foreach循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM