Control.Controls 屬性


取得控制項中包含的控制項集合。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.



下列程式碼範例會從衍生類別 PanelControl.ControlCollection 移除 Control (如果它是集合的成員)。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 控制項已加入至 FormThe 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.
   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時,每個控制項都是指派給表單之 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 方法來抓取定位順序中的上一個或下一個子控制項。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.