Control.HasChildren 属性

获取一个值,该值指示控件是否包含一个或多个子控件。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public ReadOnly Property HasChildren As Boolean
用法
Dim instance As Control
Dim value As Boolean

value = instance.HasChildren
public bool HasChildren { get; }
public:
property bool HasChildren {
    bool get ();
}
/** @property */
public boolean get_HasChildren ()
public function get HasChildren () : boolean

属性值

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

备注

如果 Controls 集合的 Count 大于零,那么 HasChildren 属性将返回 true。如果控件没有子级,那么访问 HasChildren 属性不会强制创建 Control.ControlCollection,因此,在控件树上操作时,引用该属性可提供性能好处。

示例

下面的代码示例将该控件的 BackColorForeColor 设置为默认系统颜色。如果该控件具有任何子控件,该代码将以递归方式调用它自身。该代码示例要求您有至少带有一个子控件的 Form;不过,一个具有自己的子控件的子容器控件(如 PanelGroupBox)可以更好地阐释递归。

' 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 Me.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
// 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(this.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:
   void ResetAllControlsBackColor( Control^ control )
   {
      control->BackColor = SystemColors::Control;
      control->ForeColor = SystemColors::ControlText;
      if ( this->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.set_BackColor(SystemColors.get_Control());
    control.set_ForeColor(SystemColors.get_ControlText());
    if (this.get_HasChildren()) {
        for (int iCtr=0; iCtr < control.get_Controls().get_Count(); iCtr++) {
            // Recursively call this method for each child control.
            Control childControl = control.get_Controls().get_Item(iCtr);
            ResetAllControlsBackColor(childControl);
        }
    }
} //ResetAllControlsBackColor

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Control 类
Control 成员
System.Windows.Forms 命名空间
Count