Control.HasChildren 屬性

定義

取得指示控制項是否包含一或多個子控制項的值。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.

屬性

範例

下列程式碼範例會將控制項的 BackColorForeColor 設定為預設系統色彩。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:不過,子容器控制項(例如 PanelGroupBox)與它自己的子控制項,會更清楚地示範遞迴。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

備註

如果 Controls 集合具有大於零的 CountHasChildren 屬性將會傳回 trueIf the Controls collection has a Count greater than zero, the HasChildren property will return true. 如果控制項沒有子系,存取 HasChildren 屬性並不會強制建立 Control.ControlCollection,因此在流覽控制項的樹狀結構時,參考這個屬性可提供效能優勢。Accessing 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.

適用於

另請參閱