共用方式為


Control.HasChildren 屬性

定義

會取得一個值,表示該控制是否包含一個或多個子控制項。

public:
 property bool HasChildren { bool get(); };
[System.ComponentModel.Browsable(false)]
public bool HasChildren { get; }
[<System.ComponentModel.Browsable(false)>]
member this.HasChildren : bool
Public ReadOnly Property HasChildren As Boolean

屬性值

true如果控制項包含一個或多個子控制項;否則,。 false

屬性

範例

以下程式碼範例將控制項的 and ForeColor 設定BackColor為預設系統顏色。 如果控制項有任何子控制,程式碼會遞迴呼叫自己。 這個程式碼範例要求你有一個Form至少有一個子控制項;然而,若用子容器控制,例如PanelGroupBox一個或,並擁有自己的子控制,會更有效地展示遞迴。

   // Reset all the controls to the user's default Control color.
private:
   void ResetAllControlsBackColor( Control^ control )
   {
      control->BackColor = SystemColors::Control;
      control->ForeColor = SystemColors::ControlText;
      if ( control->HasChildren )
      {
         // Recursively call this method for each child control.
         IEnumerator^ myEnum = control->Controls->GetEnumerator();
         while ( myEnum->MoveNext() )
         {
            Control^ childControl = safe_cast<Control^>(myEnum->Current);
            ResetAllControlsBackColor( childControl );
         }
      }
   }
// Reset all the controls to the user's default Control color. 
private void ResetAllControlsBackColor(Control control)
{
   control.BackColor = SystemColors.Control;
   control.ForeColor = SystemColors.ControlText;
   if(control.HasChildren)
   {
      // Recursively call this method for each child control.
      foreach(Control childControl in control.Controls)
      {
         ResetAllControlsBackColor(childControl);
      }
   }
}
' Reset all the controls to the user's default Control color. 
Private Sub ResetAllControlsBackColor(control As Control)
   control.BackColor = SystemColors.Control
   control.ForeColor = SystemColors.ControlText
   If control.HasChildren Then
      ' Recursively call this method for each child control.
      Dim childControl As Control
      For Each childControl In  control.Controls
         ResetAllControlsBackColor(childControl)
      Next childControl
   End If
End Sub

備註

若集合 ControlsCount 於零, HasChildren 則該性質將返回 true。 若控制節點沒有子節點,存取 HasChildren 該屬性不會強制建立 a Control.ControlCollection ,因此在執行控制樹時,參考此屬性可帶來效能上的提升。

適用於

另請參閱