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 集合的 Count 大于零,HasChildren 属性将返回 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.

适用于

另请参阅