Add Controls to a Form - Windows Forms .NET (2024)

  • Article

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.

The primary way a control is added to a form is through the Visual Studio Designer, but you can also manage the controls on a form at run time through code.

Important

The Desktop Guide documentation for .NET 7 and .NET 6 is under construction.

Add with Designer

Visual Studio uses the Forms Designer to design forms. There is a Controls pane which lists all the controls available to your app. You can add controls from the pane in two ways:

Add the control by double-clicking

When a control is double-clicked, it is automatically added to the current open form with default settings.

Add Controls to a Form - Windows Forms .NET (1)

Add the control by drawing

Select the control by clicking on it. In your form, drag-select a region. The control will be placed to fit the size of the region you selected.

Add Controls to a Form - Windows Forms .NET (2)

Add with code

Controls can be created and then added to a form at run time with the form's Controls collection. This collection can also be used to remove controls from a form.

The following code adds and positions two controls, a Label and a TextBox:

Label label1 = new Label(){ Text = "&First Name", Location = new Point(10, 10), TabIndex = 10};TextBox field1 = new TextBox(){ Location = new Point(label1.Location.X, label1.Bounds.Bottom + Padding.Top), TabIndex = 11};Controls.Add(label1);Controls.Add(field1);
Dim label1 As New Label With {.Text = "&First Name", .Location = New Point(10, 10), .TabIndex = 10}Dim field1 As New TextBox With {.Location = New Point(label1.Location.X, label1.Bounds.Bottom + Padding.Top), .TabIndex = 11}Controls.Add(label1)Controls.Add(field1)

See also

Add Controls to a Form - Windows Forms .NET (2024)

FAQs

How can you add controls to a form? ›

Add the control by drawing

Select the control by clicking on it. In your form, drag-select a region. The control will be placed to fit the size of the region you selected.

How do I add dynamic controls in Windows form? ›

Add controls to the form
  1. Add the following code to add each object to the Controls array of the form at the end of Form1_Load : C# Copy. //Add controls to the form. this. Controls. Add(btnAdd); this. Controls. Add(txtBox); this. Controls. Add(lstBox); this. Controls. Add(chkBox); this. Controls. ...
  2. Save the project.
May 7, 2022

How do I add controls to a form in Visual Studio? ›

Add controls to your application
  1. Open Visual Studio. ...
  2. In the Windows Forms Designer, select the TableLayoutPanel you added in the previous tutorial. ...
  3. On the left side of the Visual Studio IDE, select the Toolbox tab. ...
  4. Double-click PictureBox to add a PictureBox control to your form.
Mar 29, 2024

How to add user control to Windows form C#? ›

Add a user control to a project
  1. In Visual Studio, find the Project Explorer pane. Right-click on the project and choose Add > User Control (Windows Forms).
  2. In the Name box, type a name for your user control. Visual Studio provides a default and unique name that you may use. Next, press Add.
Apr 19, 2024

Top Articles
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 6521

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.