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
How to Develop a Courier Management System | cTrunk
About Oracle Solaris Unified Archives Using Unified Archives for System Recovery and Cloning in Oracle® Solaris 11.4
Tesla Supercharger La Crosse Photos
Chris wragge hi-res stock photography and images - Alamy
Watch Mashle 2nd Season Anime Free on Gogoanime
Dee Dee Blanchard Crime Scene Photos
How to Type German letters ä, ö, ü and the ß on your Keyboard
Barstool Sports Gif
Apnetv.con
Autozone Locations Near Me
Locate Td Bank Near Me
Horned Stone Skull Cozy Grove
What’s the Difference Between Cash Flow and Profit?
Oppenheimer Showtimes Near Cinemark Denton
Everything You Need to Know About Holly by Stephen King
Cooktopcove Com
Betonnen afdekplaten (schoorsteenplaten) ter voorkoming van lekkage schoorsteen. - HeBlad
O'reilly's Auto Parts Closest To My Location
Hood County Buy Sell And Trade
Uc Santa Cruz Events
fort smith farm & garden - craigslist
N2O4 Lewis Structure & Characteristics (13 Complete Facts)
Grayling Purnell Net Worth
Google Doodle Baseball 76
Eine Band wie ein Baum
Cvs El Salido
WRMJ.COM
Pacman Video Guatemala
Duke University Transcript Request
24 Hour Drive Thru Car Wash Near Me
Ravens 24X7 Forum
Kaiserhrconnect
آدرس جدید بند موویز
#1 | Rottweiler Puppies For Sale In New York | Uptown
Chs.mywork
Planet Fitness Lebanon Nh
Dmitri Wartranslated
The disadvantages of patient portals
Elisabeth Shue breaks silence about her top-secret 'Cobra Kai' appearance
Spectrum Outage in Genoa City, Wisconsin
Tillman Funeral Home Tallahassee
Lake Kingdom Moon 31
Ladyva Is She Married
Lyndie Irons And Pat Tenore
Stranahan Theater Dress Code
Hello – Cornerstone Chapel
Rocket League Tracker: A useful tool for every player
City Of Irving Tx Jail In-Custody List
Union Supply Direct Wisconsin
Spongebob Meme Pic
Taterz Salad
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.