Control.Controls 属性

定义

获取包含在控件内的控件的集合。

public:
 property System::Windows::Forms::Control::ControlCollection ^ Controls { System::Windows::Forms::Control::ControlCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.Control.ControlCollection Controls { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Controls : System.Windows.Forms.Control.ControlCollection
Public ReadOnly Property Controls As Control.ControlCollection

属性值

Control.ControlCollection,它表示控件内包含的控件的集合。

属性

示例

下面的代码示例从Control.ControlCollection派生类Panel的 中移除 Control (如果它是 集合的成员)。 该示例要求已在 上创建了 PanelButton和至少一个 RadioButtonForm控件。 控件RadioButton () 添加到 Panel 控件,并将 Panel 控件添加到 。Form 单击按钮时,将从 中删除Control.ControlCollection名为 的单选removeButton按钮。

   // 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 );
      }
   }
// 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);
   }
}
' 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(RemoveButton) Then
        Panel1.Controls.Remove(RemoveButton)
    End If
End Sub

注解

Control可以充当控件集合的父级。 例如,将多个控件添加到 时Form,每个控件都是分配给Controls窗体的 属性(派生自 Control 类)的 的成员Control.ControlCollection

可以使用 类中 Control.ControlCollection 提供的方法操作分配给 Controls 属性的 中的 Control.ControlCollection 控件。

将多个控件添加到父控件时,建议在初始化要添加的控件之前调用 SuspendLayout 方法。 将控件添加到父控件后,调用 ResumeLayout 方法。 这样做将提高具有许多控件的应用程序的性能。

Controls使用 属性可循环访问窗体的所有控件,包括嵌套控件。 GetNextControl使用 方法检索 Tab 键顺序中的上一个或下一个子控件。 ActiveControl使用 属性获取或设置容器控件的活动控件。

适用于

另请参阅