foreach循环未检测到任何控件(图片框)

发布于 2025-01-28 20:37:01 字数 647 浏览 6 评论 0原文

以下代码应该检测Controls属性中的所有图像框,并将碰撞应用到它。 (这是CompSci最终项目的Windows形式)。问题在于它没有检测到任何图片框。否则,代码运行。有什么想法吗?

foreach (Control x in this.Controls)
            {
                if (x is PictureBox)
                {
                    if ((string)x.Tag == "platform")
                    {
                        if (PlayerPB.Bounds.IntersectsWith(x.Bounds))
                        {
                            force = 8;
                            PlayerPB.Top = x.Top - PlayerPB.Height;
                        }

                        x.BringToFront();
                    }
                }
            }

This following code is supposed to detect all PictureBoxes in the Controls property, and apply collisions to it. (This is in Windows Form for a CompSci final project). The problem is that it is not detecting any PictureBoxes; otherwise, the code runs. Any ideas?

foreach (Control x in this.Controls)
            {
                if (x is PictureBox)
                {
                    if ((string)x.Tag == "platform")
                    {
                        if (PlayerPB.Bounds.IntersectsWith(x.Bounds))
                        {
                            force = 8;
                            PlayerPB.Top = x.Top - PlayerPB.Height;
                        }

                        x.BringToFront();
                    }
                }
            }

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

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

发布评论

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

评论(1

木森分化 2025-02-04 20:37:01

您需要在主要形式的每个容器中重复出现,并查看其中是否有任何图像框:

private void WirePBs(Control container) 
{
    foreach (Control x in container.Controls)
    {
        if (x is PictureBox) && (string)x.Tag == "platform")
        {
            if (PlayerPB.Bounds.IntersectsWith(x.Bounds))
            {
                force = 8;
                PlayerPB.Top = x.Top - PlayerPB.Height;
            }
            x.BringToFront();
        }
        else if (x.HasChildren)
        {
             WirePBs(x);
        }
    }
}

然后,您可以通过以形式本身传递来调用它:

WirePBs(this);

You need to recurse into each container within the main form and see if there are any PictureBoxes within there:

private void WirePBs(Control container) 
{
    foreach (Control x in container.Controls)
    {
        if (x is PictureBox) && (string)x.Tag == "platform")
        {
            if (PlayerPB.Bounds.IntersectsWith(x.Bounds))
            {
                force = 8;
                PlayerPB.Top = x.Top - PlayerPB.Height;
            }
            x.BringToFront();
        }
        else if (x.HasChildren)
        {
             WirePBs(x);
        }
    }
}

Then you can call it by passing in the form itself:

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