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 에서는 자식 컨트롤이 하나 이상 있어야 합니다. 그러나 자식 컨트롤이 있는 GroupBox자식 컨테이너 컨트롤(예: Panel 자식 컨트롤)은 재귀를 더 잘 보여 줍니다.

   // 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

설명

컬렉션 CountControls 0보다 크면 속성이 HasChildren 반환true됩니다. 속성에 HasChildren 액세스해도 컨트롤에 자식이 없으면 강제로 생성 Control.ControlCollection 되지 않으므로 이 속성을 참조하면 컨트롤 트리를 걸을 때 성능이 향상됩니다.

적용 대상

추가 정보