NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Adding Controls to Win Forms

Most forms are designed by adding controls to the surface of the form to define a user interface. A control is a component on a form used to display information or accept user input.

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

To add a control to a form programmatically

  1. In your code editor, open the form that you want to add controls to.
  2. Create a Private member variable for the Button within your class:
    [Visual Basic]
    Private Button1 As Button
    [C#]
    private Button Button1;
  3. In the method that handles the 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 Size and Location, and add the control:
    [Visual Basic]
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
       Dim MyText as New Textbox
       MyText.Size = New Point(25,125)
       MyText.Location = New Point(25,25)
       Me.Controls.Add MyText
    End Sub
    [C#]
    private void Button1_Click(object sender, EventArgs e) {
        Textbox MyText = new Textbox();
        MyText.Size = new Size(25,125);
        MyText.Location = new Point(25,25);
        this.Controls.Add (MyText);
    }
    Note   You can also add code to initialize other properties of the control.

See Also

Control Manipulation on Win Forms | Anchoring Controls on Win Forms | Docking Controls on Win Forms | Layering Objects on Win Forms | Positioning Controls on Win Forms | Resizing Controls on Win Forms | Setting the Tab Order on Win Forms | Setting the Text Displayed by a Control | Controls You can Use on Win Forms