获取包含在控件内的控件的集合。
[Visual Basic]
Public ReadOnly Property Controls As Control.ControlCollection
[C#]
public Control.ControlCollection Controls {get;}
[C++]
public: __property Control.ControlCollection* get_Controls();
[JScript]
public function get Controls() : Control.ControlCollection;
属性值
一个 Control.ControlCollection,它表示控件内包含的控件的集合。
备注
Control 可以充当控件集合的父级。例如,当将多个控件添加到 Form 时,每一个控件都是 Control.ControlCollection 对象的成员,该对象被分配给窗体的 Controls 属性,窗体从 Control 类派生而来。
可以使用 Control.ControlCollection 类的方法,在分配给 Controls 属性的 Control.ControlCollection 对象中操作控件。
将多个控件添加到父控件时,建议在初始化要添加的控件之前调用 SuspendLayout 方法。将控件添加到父控件之后, 调用 ResumeLayout 方法。这样就可以提高带有许多控件的应用程序的性能。
示例
[Visual Basic, C#, C++] 下面的示例从派生类 Panel 的 Control.ControlCollection 中移除一个 Control(如果它是该集合的成员)。该示例假定您已在 Form 上创建了一个 Panel、一个 Button 以及至少一个 RadioButton 控件。将 RadioButton 控件添加到 Panel 控件,而将 Panel 控件添加到 Form。单击该按钮 时,从 Control.ControlCollection 中移除名为 radioButton2 的单选按钮。
[Visual Basic]
' Remove the RadioButton control if it exists.
Private Sub RemoveButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RemoveButton.Click
If Panel1.Controls.Contains(RadioAddRangeButton) Then
Panel1.Controls.Remove(RadioAddRangeButton)
End If
End Sub
[C#]
// Remove the RadioButton control if it exists.
private void removeButton_Click(object sender, System.EventArgs e)
{
if(panel1.Controls.Contains(removeButton))
{
panel1.Controls.Remove(removeButton);
}
}
[C++]
// Remove the RadioButton control if it exists.
private:
void removeButton_Click(Object* sender, System::EventArgs* e) {
if (panel1->Controls->Contains(removeButton)) {
panel1->Controls->Remove(removeButton);
}
}