[英]How do I know that picturebox has child controls
I made a little game with c# , but there was a problem. 我用c#做了一个小游戏,但是有一个问题。 I put a
Label
in the PictureBox
, but I don't know how to tell that there's a Label
in the PictureBox
. 我在
PictureBox
放置了一个Label
,但是我不知道如何判断PictureBox
中有一个Label
。
pbArray[x,y].Controls == label; // ???
Let's put the question in other words: 让我们换句话说:
If there's
Any
Label
within the picture box'sControls
?如果有
Any
Label
的图片框的内部Controls
?
We can asnwer it with an easy Linq : 我们可以用一个简单的Linq来解决它:
using System.Linq;
...
bool hasLabel = pbArray[x,y]
.Controls
.OfType<Label>()
.Any();
However, we can put the question in a different way: 但是,我们可以用其他方式提出问题:
Do we have
Any
Label
which overlaps (ie just painted over or under the picture box; not necessary within picture box'sControls
) the picture box?我们是否有
Any
Label
重叠 (即刚刚涂过或图片框下;图片框的范围内没有必要的Controls
)的图片框?
In this case we have to implement more code: 在这种情况下,我们必须实现更多代码:
private static bool AreOverlapped(Rectangle left, Rectangle right) {
//TODO: put relevant code here
}
...
Rectangle boxRect = new Rectangle(
pbArray[x,y].Parent.PointToScreen(pbArray[x,y].Location),
pbArray[x,y].Size);
bool hasOverlappedLabel = this
.Controls // <- Labels that are directly on the form only
.OfType<Label>()
.Select(lbl => new Rectangle(
lbl.Parent.PointToScreen(lbl.Location),
lbl.Size
))
.Any(rect => AreOverlapped(rect, boxRect));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.