Control.HasChildren プロパティ

定義

コントロールに 1 つ以上の子コントロールが格納されているかどうかを示す値を取得します。

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

コントロールに 1 つ以上の子コントロールが格納されている場合は true。それ以外の場合は false

属性

次のコード例では、 BackColor コントロールと ForeColor コントロールの色を既定のシステム 色に設定します。 コントロールに子コントロールがある場合、コードは再帰的に自身を呼び出します。 このコード例では、少なくとも 1 つの子コントロールを持つ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このプロパティは .true プロパティに HasChildren アクセスしても、コントロールに子がない場合は強制的に作成 Control.ControlCollection されません。そのため、このプロパティを参照すると、コントロールのツリーを歩くときにパフォーマンスの利点が得られます。

適用対象

こちらもご覧ください