foreach循环未检测到任何控件(图片框)
以下代码应该检测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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在主要形式的每个容器中重复出现,并查看其中是否有任何图像框:
然后,您可以通过以形式本身传递来调用它:
You need to recurse into each container within the main form and see if there are any PictureBoxes within there:
Then you can call it by passing in the form itself: