Control.HasChildren 屬性
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得指示控制項是否包含一或多個子控制項的值。
public:
property bool HasChildren { bool get(); };
C#
[System.ComponentModel.Browsable(false)]
public bool HasChildren { get; }
[<System.ComponentModel.Browsable(false)>]
member this.HasChildren : bool
Public ReadOnly Property HasChildren As Boolean
如果控制項包含一或多個子控制項,則為 true
,否則為 false
。
- 屬性
下列程式碼範例會將 BackColor 控制項的 和 ForeColor 設定為預設系統色彩。 如果控制項有任何子控制項,程式碼會以遞迴方式呼叫本身。 此程式碼範例需要您具有 Form 至少一個子控制項的 ;不過,子容器控制項,例如 Panel 或 GroupBox ,其本身的子控制項 () 會更能示範遞迴。
// 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 );
}
}
}
C#
// 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 ,因此參考這個屬性可以在執行控制項樹狀結構時提供效能優勢。
產品 | 版本 |
---|---|
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8 |
Windows Desktop | 3.0, 3.1, 5, 6, 7 |