简体   繁体   English

我怎么知道Picturebox有子控件

[英]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's Controls ? 如果有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's Controls ) 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM