如何使用变量定位对象

发布于 2024-12-03 02:42:56 字数 836 浏览 2 评论 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;
    }                           
}

I'm trying to create 81 picture boxes and have them automatically positioned a certain distance apart from one another but they don't seem to be placing in any logical order. I have to initialize the X point to -1700 for them to even appear on the screen. The following code gets the first 15 where I want them but then they start stacking on top of one another instead of continuing the pattern. This is the result of about an hour of tinkering but initially the logic looked fine. I even had a message box that would display the current X,Y that was being set and it was correct it just would not place them at those coordinates.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩心不撩汉 2024-12-10 02:42:56

使用 FlowLayoutPanel 代替手动放置元素。将控件添加到面板并让它为您进行安排。

Instead of manually placing elements use a FlowLayoutPanel. Add the controls to the panel and let it do the arrangement for you.

祁梦 2024-12-10 02:42:56

这段代码按照您的预期工作

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 控件

This code works as you are expecting

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

        }

enter image description here

But I would go with something like @Ed said, a FlowLayout control

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文