Export (0) Print
Expand All

Form.ControlCollection.Add Method

Adds a control to the form.

[Visual Basic]
Overrides Public Sub Add( _
   ByVal value As Control _
)
[C#]
public override void Add(
 Control value
);
[C++]
public: void Add(
 Control* value
);
[JScript]
public override function Add(
   value : Control
);

Parameters

value
The Control to add to the form.

Exceptions

Exception Type Condition
Exception A Multiple Document Interface (MDI) parent form cannot have controls added to it.

Remarks

You can use this method to adds controls to the form. If you want to add a group of already created controls to the form, use the ControlCollection.AddRange method of the Control.ControlCollection class.

Example

[Visual Basic, C#, C++] The following example adds a TextBox and Label control to the control collection of a form. The example assumes that a form has been created and named Form1.

[Visual Basic] 
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

[C#] 
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);
 }
 

[C++] 
public:
void AddMyControls()
 {
    TextBox* textBox1 = new TextBox();
    Label* label1 = new Label();
    
    // Initialize the controls and their bounds.
    label1->Text = S"First Name";
    label1->Location = Point(48,48);
    label1->Size = System::Drawing::Size(104, 16);
    textBox1->Text = S"";
    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);
 }
 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Form.ControlCollection Class | Form.ControlCollection Members | System.Windows.Forms Namespace | Remove

Show:
© 2015 Microsoft