Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Windows Forms
 How to: Add Controls to Windows For...
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Windows Forms Programming
How to: Add Controls to Windows Forms

Updated: November 2007

Most forms are designed by adding controls to the surface of the form to define a user interface (UI). A control is a component on a form used to display information or accept user input. For more information about controls, see Windows Forms Controls.

Note:

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To draw a control on a form

  1. Open the form. For more information, see How to: Display Windows Forms in the Designer.

  2. In the Toolbox, click the control you want to add to your form.

  3. On the form, click where you want the upper-left corner of the control to be located, and drag to where you want the lower-right corner of the control to be located.

    The control is added to the form with the specified location and size.

    Note:

    Each control has a default size defined. You can add a control to your form in the control's default size by dragging it from the Toolbox to the form.

To drag a control to a form

  1. Open the form. For more information, see How to: Display Windows Forms in the Designer.

  2. In the Toolbox, click the control you want and drag it to your form.

    The control is added to the form at the specified location in its default size.

    Note:

    You can double-click a control in the Toolbox to add it to the upper-left corner of the form in its default size.

    You can also add controls dynamically to a form at run time. In the following code example, a TextBox control will be added to the form when a Button control is clicked.

    Note:

    The following procedure requires the existence of a form with a Button control, Button1, already placed on it.

To add a control to a form programmatically

  • In the method that handles the button's Click event within your form's class, insert code similar to the following to add a reference to your control variable, set the control's Location, and add the control.

    Visual Basic
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim MyText As New TextBox()
       MyText.Location = New Point(25, 25)
       Me.Controls.Add(MyText)
    End Sub
    

    C#
    private void button1_Click(object sender, System.EventArgs e) 
    {
       TextBox myText = new TextBox();
       myText.Location = new Point(25,25);
       this.Controls.Add (myText);
    }
    

    J#
    TextBox myText = new TextBox();
    myText.set_Location(new Point(25, 25));
    this.get_Controls().Add(myText);
    

    Visual C++
    private:
      System::Void button1_Click(System::Object ^  sender,
        System::EventArgs ^  e)
      {
        TextBox ^ myText = gcnew TextBox();
        myText->Location = Point(25,25);
        this->Controls->Add(myText);
      }
    
    Note:

    You can also add code to initialize other properties of the control.

    Security Note:

    You might expose your local computer to a security risk through the network by referencing a malicious UserControl. This would only be a concern in the case of a malicious person creating a damaging custom control, followed by you mistakenly adding it to your project.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker