Bagikan melalui


Control.HasChildren Properti

Definisi

Mendapatkan nilai yang menunjukkan apakah kontrol berisi satu atau beberapa kontrol anak.

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

Nilai Properti

true jika kontrol berisi satu atau beberapa kontrol anak; jika tidak, false.

Atribut

Contoh

Contoh kode berikut mengatur BackColor dan ForeColor kontrol ke warna sistem default. Kode secara rekursif memanggil dirinya sendiri jika kontrol memiliki kontrol anak. Contoh kode ini mengharuskan Anda memiliki Form setidaknya satu kontrol anak; namun, kontrol kontainer anak, seperti Panel atau GroupBox, dengan kontrol anaknya sendiri akan lebih baik menunjukkan rekursi.

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

Keterangan

Controls Jika koleksi memiliki Count lebih besar dari nol, HasChildren properti akan mengembalikan true. HasChildren Mengakses properti tidak memaksa pembuatan Control.ControlCollection jika kontrol tidak memiliki turunan, jadi merujuk properti ini dapat memberikan manfaat performa saat berjalan di pohon kontrol.

Berlaku untuk

Lihat juga