Form.ControlCollection.Add(Control) Metod

Definition

Lägger till en kontroll i formuläret.

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)

Parametrar

value
Control

Att Control lägga till i formuläret.

Undantag

Ett överordnat MDI-formulär (flera dokumentgränssnitt) kan inte ha kontroller tillagda.

Exempel

Följande kodexempel lägger till en TextBox och-kontroll Label i kontrollsamlingen för ett formulär. Exemplet kräver att ett formulär har skapats och fått namnet 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

Kommentarer

Du kan använda den här metoden för att lägga till kontroller i formuläret. Om du vill lägga till en grupp kontroller som redan har skapats i formuläret använder du Control.ControlCollection.AddRange -metoden för Control.ControlCollection klassen.

Gäller för

Se även