Form.ControlCollection 类

表示窗体上控件的集合。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<ComVisibleAttribute(False)> _
Public Class ControlCollection
    Inherits ControlCollection
用法
Dim instance As ControlCollection
[ComVisibleAttribute(false)] 
public class ControlCollection : ControlCollection
[ComVisibleAttribute(false)] 
public ref class ControlCollection : public ControlCollection
/** @attribute ComVisibleAttribute(false) */ 
public class ControlCollection extends ControlCollection
ComVisibleAttribute(false) 
public class ControlCollection extends ControlCollection

备注

此类表示在某窗体内包含的控件的集合。可以使用 Add 方法向窗体添加控件,也可以使用 Remove 方法从窗体移除控件。如果没有将该控件集合绑定到特定窗体,则无法创建此类表示的控件集合。因此,也就无法创建此控件集合的多个实例,无法将它们与活动窗体交换以提供不同的控件布局。

示例

下面的代码示例向窗体的控件集合添加一个 TextBox 控件和一个 Label 控件。该示例要求先创建一个名为 Form1 的窗体。

Public Sub AddMyControls()
    Dim textBox1 As New TextBox()
    Dim label1 As New Label()
    
    ' Initialize the controls and their bounds.
    label1.Text = "First Name"
    label1.Location = New Point(48, 48)
    label1.Size = New Size(104, 16)
    textBox1.Text = ""
    textBox1.Location = New Point(48, 64)
    textBox1.Size = New Size(104, 16)
    
    ' Add the TextBox control to the form's control collection.
    Controls.Add(textBox1)
    ' Add the Label control to the form's control collection.
    Controls.Add(label1)
End Sub 'AddMyControls
public void AddMyControls()
 {
    TextBox textBox1 = new TextBox();
    Label label1 = new Label();
    
    // Initialize the controls and their bounds.
    label1.Text = "First Name";
    label1.Location = new Point(48,48);
    label1.Size = new Size (104, 16);
    textBox1.Text = "";
    textBox1.Location = new Point(48, 64);
    textBox1.Size = new Size(104,16);
 
    // Add the TextBox control to the form's control collection.
    Controls.Add(textBox1);
    // Add the Label control to the form's control collection.
    Controls.Add(label1);
 }
 
public:
   void AddMyControls()
   {
      TextBox^ textBox1 = gcnew TextBox;
      Label^ label1 = gcnew Label;
      
      // Initialize the controls and their bounds.
      label1->Text = "First Name";
      label1->Location = Point( 48, 48 );
      label1->Size = System::Drawing::Size( 104, 16 );
      textBox1->Text = "";
      textBox1->Location = Point(48,64);
      textBox1->Size = System::Drawing::Size( 104, 16 );
      
      // Add the TextBox control to the form's control collection.
      Controls->Add( textBox1 );
      // Add the Label control to the form's control collection.
      Controls->Add( label1 );
   }

继承层次结构

System.Object
   System.Windows.Forms.Layout.ArrangedElementCollection
     System.Windows.Forms.Control.ControlCollection
      System.Windows.Forms.Form.ControlCollection

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Form.ControlCollection 成员
System.Windows.Forms 命名空间
Control.ControlCollection 类