在C#中对齐数组中的标签以形成网格

在C#中对齐数组中的标签以形成网格,c#,label,picturebox,C#,Label,Picturebox,我已经创建了一组黑色标签,并将它们显示在一个图片框中。不幸的是,我无法在黑线的每个交点处直接将它们对齐。我该怎么做 InitializeComponent(); int x = pictureBox1.Location.X; int y = pictureBox1.Location.Y; // create 361 labels, set their properties for (int i = 0; i < 361;

我已经创建了一组黑色标签,并将它们显示在一个图片框中。不幸的是,我无法在黑线的每个交点处直接将它们对齐。我该怎么做

InitializeComponent();

        int x = pictureBox1.Location.X;

        int y = pictureBox1.Location.Y;

        // create 361 labels, set their properties
        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].Text = "0";
            board[i].BackColor = Color.Black;
            //set size of labels
            board[i].Size = new Size(31,31);

        }


        // set the position of the label
        foreach (Label i in board)
        {
            //set distance between labels
            if (x >= 1024)
            {
                x = pictureBox1.Location.X;
                y += 52;
            }

            else
            {
                x += 52;
            }


            this.Controls.Add(i);
            i.BringToFront();
            i.Location = new Point(x, y);
        }
InitializeComponent();
int x=pictureBox1.Location.x;
int y=pictureBox1.Location.y;
//创建361个标签,设置其属性
对于(int i=0;i<361;i++)
{
板[i]=新标签();
板[i]。父级=pictureBox1;
板[i]。位置=新点(x,y);
单板[i].Name=“label”+i;
板[i]。Text=“0”;
板[i]。背景色=颜色。黑色;
//设置标签的大小
板[i]。尺寸=新尺寸(31,31);
}
//设置标签的位置
foreach(板上的标签i)
{
//设置标签之间的距离
如果(x>=1024)
{
x=图片box1.Location.x;
y+=52;
}
其他的
{
x+=52;
}
本.控件.添加(i);
i、 布林托夫隆();
i、 位置=新点(x,y);
}

据我所知,您的问题和代码

您正在同时创建标签
位置(x,y)
其中
x=100
y=0

在下一个循环中

    foreach (Label i in board)
    {
        if (x >= 1024)
        {
            x = 0;
            y += i.Height + 55;
        }

        else if (y >= 1024)
        {
            y = 0;
            x += i.Width + 55;
        }
    }
你的条件都不会变为真,因为你的x=100,y=0 因此,位置将保持不变,所有标签将位于同一位置

如果要显示国际象棋网格,请参见此方法

如果要在线的交点上显示标签,请允许修改代码

        x = PictureBox1.Location.X + 55;
        y = pictureBox1.Location.Y + 55;
        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].Text = "0";
                board[i].BackColor = Color.Black;
                board[i].Size = new Size(55,55); //Define size of label according to your choice 
                if(x >= 1024)
                {
                  x = PictureBox1.Location.X + 55; //Start position
                  y += 55;                    // Step to next line
                }
                else
                  x += 55;                   //jump to next horizontal box
            }
x=PictureBox1.Location.x+55;
y=pictureBox1.位置y+55;
对于(int i=0;i<361;i++)
{
板[i]=新标签();
板[i]。父级=pictureBox1;
板[i]。位置=新点(x,y);
单板[i].Name=“label”+i;
板[i]。Text=“0”;
板[i]。背景色=颜色。黑色;
board[i].Size=新尺寸(55,55);//根据您的选择定义标签的尺寸
如果(x>=1024)
{
x=PictureBox1.Location.x+55;//开始位置
y+=55;//转到下一行
}
其他的
x+=55;//跳转到下一个水平框
}

我希望这有帮助

那么我应该将x和y设置为什么呢?我不能简单地将其声明为没有值,也不能在任何循环中声明值。每边相隔55,我想在linesHow的每个交点上将它们显示为片段。我可以正确地排列它们吗?您必须通过定义边界来管理它。我不知道他们在你身边是什么。更改标签的大小。当我使用它作为10 x 10时,改变跳跃的值,就像我使用它作为55一样。根据你的需要调整,兄弟。我可以给你一些提示,帮你找出你的问题。如果我能帮助你,那么你可以把它标记为答案,剩下的就是你这边的逻辑。但这里有一个建议:如果你计划开发一个国际象棋游戏,那么这个逻辑很难实现。检查我已经给你的链接。但是如果你在做别的事情,那就去做吧