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,表示控制項中包含之控制項集合。

屬性

範例

如果衍生類別是集合的成員,下列程式碼範例會 ControlControl.ControlCollection 衍生類別 Panel 的 移除 。 此範例會要求您已在 上 Form 建立 、 PanelButton 和 至少一個 RadioButton 控制項。 控制項 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.ControlCollection ,衍生自 Control 類別。

您可以使用 類別中 Control.ControlCollection 可用的方法來操作指派給 Controls 屬性的 Control.ControlCollection 控制項。

將數個控制項加入父控制項時,建議您先呼叫 SuspendLayout 方法,再初始化要加入的控制項。 將控制項新增至父控制項之後,請呼叫 ResumeLayout 方法。 這樣做會增加具有許多控制項的應用程式效能。

Controls使用 屬性逐一查看表單的所有控制項,包括巢狀控制項。 GetNextControl使用 方法來擷取定位順序中的上一個或下一個子控制項。 ActiveControl使用 屬性來取得或設定容器控制項的使用中控制項。

適用於

另請參閱