0

我正在尝试创建 81 个图片框,并让它们自动放置在彼此之间一定距离的位置,但它们似乎没有按任何逻辑顺序排列。我必须将 X 点初始化为 -1700 才能让它们出现在屏幕上。以下代码获得了我想要的前 15 个,但随后它们开始相互堆叠,而不是继续模式。这是大约一个小时修修补补的结果,但最初逻辑看起来不错。我什至有一个消息框,可以显示当前正在设置的 X、Y,它是正确的,它只是不会将它们放在那些坐标处。

int X = -1700;
int Y = 0;

for (int i = 0; i < 81; i++)
{
    this.Controls.Add(championThumbNailsArray[i]);
    championThumbNailsArray[i].Height = 80;
    championThumbNailsArray[i].Width = 80;
    championThumbNailsArray[i].Location = new Point(X, Y);
   // MessageBox.Show(Convert.ToString(X) + "," + Convert.ToString(Y));
    championThumbNailsArray[i].ImageLocation = akali.grabPicture();
    //championThumbNailsArray[i].ImageLocation = championsArray[i].grabPicture();
    if (X <= 425)
        X = X + 85;
    else
    {
        X = -1700;
        Y = Y + 85;
    }                           
}
4

2 回答 2

1

而不是手动放置元素使用FlowLayoutPanel。将控件添加到面板并让它为您安排。

于 2011-09-02T00:31:59.563 回答
0

此代码按您的预期工作

private void Form1_Load(object sender, EventArgs e)
        {
            int x = 0;
            int y = 0;

            for (int i = 0; i < 81; i++)
            {
                PictureBox p = new PictureBox();
                p.BorderStyle = BorderStyle.Fixed3D;
                p.Height = 80;
                p.Width = 80;
                p.Location = new Point(x, y);

                x += 85;

                if (x > 425)
                {
                    x = 0;
                    y += 85;
                }

                this.Controls.Add(p);
            }

        }

在此处输入图像描述

但我会选择像@Ed 所说的那样,FlowLayout 控件

于 2011-09-02T00:39:05.560 回答