Control.HasChildren Control.HasChildren Control.HasChildren Control.HasChildren Property

定義

取得指示控制項是否包含一或多個子控制項的值。Gets a value indicating whether the control contains one or more child controls.

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

屬性值

如果控制項包含一或多個子控制項,則為 true,否則為 falsetrue if the control contains one or more child controls; otherwise, false.

範例

下列程式碼範例會將BackColor控制項ForeColor的和設定為預設系統色彩。The following code example sets the BackColor and ForeColor of the controls to the default system colors. 如果控制項有任何子控制項, 則程式碼會以遞迴方式呼叫本身。The code recursively calls itself if the control has any child controls. 這個程式碼範例要求您具有至少Form一個子控制項的, 但是子容器控制項 ( Panel例如或GroupBox) 具有自己的子控制項, 會更清楚地示範遞迴。This code example requires that you have a Form with at least one child control; however, a child container control, like a Panel or GroupBox, with its own child control(s) would better demonstrate the recursion.

   // 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

備註

如果集合具有大於零的值true, 則HasChildren屬性會傳回。 Count ControlsIf the Controls collection has a Count greater than zero, the HasChildren property will return true. 如果控制項沒有任何子系, 存取屬性並不會強制建立,因此在流覽控制項的樹狀結構時,參考這個屬性可以提供效能優勢。HasChildren Control.ControlCollectionAccessing the HasChildren property does not force the creation of a Control.ControlCollection if the control has no children, so referencing this property can provide a performance benefit when walking a tree of controls.

適用於

另請參閱