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
[Visual Basic] Private Button1 As Button [C#] private Button Button1;
[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.
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