Control.Controls Control.Controls Control.Controls Control.Controls Property


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

 property System::Windows::Forms::Control::ControlCollection ^ Controls { System::Windows::Forms::Control::ControlCollection ^ get(); };
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 Control.ControlCollection从派生类的派生类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. 该示例要求你已在中创建Panel了一个Button、一个和Form至少一个RadioButton控件。The example requires that you have created a Panel, a Button, and at least one RadioButton control on a Form. 将控件添加Panel到控件,并FormPanel控件添加到。 RadioButtonThe RadioButton control(s) are added to the Panel control, and the Panel control added to the Form. 单击该按钮时,会removeButton Control.ControlCollection从中删除名为的单选按钮。When the button is clicked, the radio button named removeButton is removed from the Control.ControlCollection.

   // Remove the RadioButton control if it exists.
   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)
  ' 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
      End If
  End Sub


Control可充当控件集合的父级。A Control can act as a parent to a collection of controls. 例如,在将多个Form控件添加到中时,每个控件都是Control.ControlCollection分配给Controls窗体的属性的成员,该属性派生自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 Control.ControlCollection类中提供的方法来操作Controls分配给属性的中的控件。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.