簡體   English   中英

我怎么知道Picturebox有子控件

[英]How do I know that picturebox has child controls

我用c#做了一個小游戲,但是有一個問題。 我在PictureBox放置了一個Label ,但是我不知道如何判斷PictureBox中有一個Label

pbArray[x,y].Controls == label; // ???

讓我們換句話說:

如果有Any Label的圖片框的內部 Controls

我們可以用一個簡單的Linq來解決它:

 using System.Linq;

 ...

 bool hasLabel = pbArray[x,y]
   .Controls
   .OfType<Label>()
   .Any();  

但是,我們可以用其他方式提出問題:

我們是否有Any Label 重疊 (即剛剛過或圖片框下;圖片框的范圍內沒有必要的Controls )的圖片框?

在這種情況下,我們必須實現更多代碼:

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