Control.Controls 属性

定义

获取包含在控件内的控件的集合。Gets the collection of controls contained within the control.

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; }
member this.Controls : System.Windows.Forms.Control.ControlCollection
Public ReadOnly Property Controls As Control.ControlCollection

属性值

Control.ControlCollection,它表示控件内包含的控件的集合。A Control.ControlCollection representing the collection of controls contained within the control.

属性

示例

下面的代码示例从派生类的 Control.ControlCollection 中移除 Control Panel 如果它是集合的成员。The following code example removes a Control from the Control.ControlCollection of the derived class Panel if it is a member of the collection. 该示例要求您已在 Form上创建了一个 Panel、一个 Button和至少一个 RadioButton 控件。The example requires that you have created a Panel, a Button, and at least one RadioButton control on a Form. RadioButton 控件添加到 Panel 控件,并将 Panel 控件添加到 Form中。The RadioButton control(s) are added to the Panel control, and the Panel control added to the Form. 单击该按钮时,将从 Control.ControlCollection中删除名为 removeButton 的单选按钮。When the button is clicked, the radio button named removeButton is removed from the Control.ControlCollection.

   // 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 可以充当控件集合的父级。A Control can act as a parent to a collection of controls. 例如,将多个控件添加到 Form时,每个控件都是分配给窗体 Controls 属性的 Control.ControlCollection 的成员,该属性派生自 Control 类。For example, when several controls are added to a Form, each of the controls is a member of the Control.ControlCollection assigned to the Controls property of the form, which is derived from the Control class.

您可以使用 Control.ControlCollection 类中提供的方法来操作分配给 Controls 属性的 Control.ControlCollection 中的控件。You can manipulate the controls in the Control.ControlCollection assigned to the Controls property by using the methods available in the Control.ControlCollection class.

向父控件添加几个控件时,建议您在初始化要添加的控件之前调用 SuspendLayout 方法。When adding several controls to a parent control, it is recommended that you call the SuspendLayout method before initializing the controls to be added. 向父控件添加控件后,调用 ResumeLayout 方法。After adding the controls to the parent control, call the ResumeLayout method. 这样做将提高具有许多控件的应用程序的性能。Doing so will increase the performance of applications with many controls.

使用 Controls 属性可循环访问窗体的所有控件,包括嵌套控件。Use the Controls property to iterate through all controls of a form, including nested controls. 使用 GetNextControl 方法按 tab 键顺序检索上一个或下一个子控件。Use the GetNextControl method to retrieve the previous or next child control in the tab order. 使用 ActiveControl 属性可获取或设置容器控件的活动控件。Use the ActiveControl property to get or set the active control of a container control.

适用于

另请参阅