共用方式為


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 大於零,則 HasChildren 屬性會傳回 trueHasChildren如果控制項沒有子系,則存取 屬性並不會強制建立 Control.ControlCollection ,因此參考這個屬性可以在執行控制項樹狀結構時提供效能優勢。

適用於

另請參閱