Поделиться через


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

Значение свойства

true Значение , если элемент управления содержит один или несколько дочерних элементов управления; falseв противном случае .

Атрибуты

Примеры

В следующем примере кода элементы управления задаются BackColorForeColor для системных цветов по умолчанию. Код рекурсивно вызывает себя, если элемент управления имеет дочерние элементы управления. В этом примере кода требуется, чтобы у вас был хотя бы один дочерний элемент управления. Однако дочерний 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 свойство возвращаетсяtrue. HasChildren Доступ к свойству не принудительно создаетсяControl.ControlCollection, если элемент управления не имеет дочерних элементов, поэтому ссылка на это свойство может обеспечить преимущество производительности при переходе по дереву элементов управления.

Применяется к

См. также раздел