Form.ControlCollection.Add 方法

将控件添加到窗体中。

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

语法

声明
Public Overrides Sub Add ( _
    value As Control _
)
用法
Dim instance As ControlCollection
Dim value As Control

instance.Add(value)
public override void Add (
    Control value
)
public:
virtual void Add (
    Control^ value
) override
public void Add (
    Control value
)
public override function Add (
    value : Control
)

参数

  • value
    要添加到窗体中的 Control

异常

异常类型 条件

Exception

不能向多文档界面 (MDI) 父窗体添加控件。

备注

可以使用此方法向窗体添加控件。如果要将一组已经创建的控件添加到窗体中,请使用 Control.ControlCollection 类的 Control.ControlCollection.AddRange 方法。

示例

下面的代码示例向窗体的控件集合添加一个 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 );
   }

平台

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 类
Form.ControlCollection 成员
System.Windows.Forms 命名空间
Remove