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

属性值

Boolean

如果控件包含一个或多个子控件,则为 true;否则为 false

属性

示例

下面的代码示例将 BackColor 控件和 ForeColor 控件设置为默认系统颜色。 如果控件具有任何子控件,则代码以递归方式调用自身。 此代码示例要求具有至少一个 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

注解

Controls如果集合的Count值为 0,则HasChildren属性将返回trueHasChildren如果控件没有子级,则访问该属性不会强制创建该Control.ControlCollection属性,因此在行走控件树时,引用此属性可以提供性能优势。

适用于

另请参阅