Form.ControlCollection.Add(Control) 方法

定義

將控制項加入表單中。

public:
 override void Add(System::Windows::Forms::Control ^ value);
public override void Add (System.Windows.Forms.Control value);
public override void Add (System.Windows.Forms.Control? value);
override this.Add : System.Windows.Forms.Control -> unit
Public Overrides Sub Add (value As Control)

參數

value
Control

要加入到表單中的 Control

例外狀況

多重文件介面 (MDI) 父表單中不能有控制項加入。

範例

下列程式碼範例會將 TextBoxLabel 控制項新增至表單的控制項集合。 此範例要求已建立表單並命名為 Form1

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 );
   }
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 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

備註

您可以使用這個方法將控制項新增至表單。 如果您想要將已建立的控制項群組新增至表單,請使用 Control.ControlCollection.AddRange 類別的 Control.ControlCollection 方法。

適用於

另請參閱