In Windows forms, TextBox plays an important role. With the help of TextBox, the user can enter data in the application, it can be of a single line or of multiple lines. The TextBox is a class and it is defined under System.Windows.Forms namespace. In C#, you can create a TextBox in two different ways:
1. Design-Time: It is the simplest way to create a TextBox as shown in the following steps:
- Step 1: Create a windows form. As shown in the below image:
Visual Studio -> File -> New -> Project -> WindowsFormApp

- Step 2: Drag the TextBox control from the ToolBox and drop it on the windows form. You can place TextBox anywhere on the windows form according to your need.

- Step 3: After drag and drop you will go to the properties of the TextBox control to modify the TextBox design according to your requirement.

2. Run-Time: It is a little bit trickier than the above method. In this method, you can create your own textbox using the TextBox class.
- Step 1 : Create a textbox using the TextBox() constructor provided by the TextBox class.
// Creating textbox TextBox Mytextbox = new TextBox();
- Step 2 : After creating TextBox, set the properties of the TextBox provided by the TextBox class.
// Set location of the textbox Mytextbox.Location = new Point(187, 51); // Set background color of the textbox Mytextbox.BackColor = Color.LightGray; // Set the foreground color of the textbox Mytextbox.ForeColor = Color.DarkOliveGreen; // Set the size of the textbox Mytextbox.AutoSize = true; // Set the name of the textbox Mytextbox.Name = "text_box1";
- Step 3 : And last add this textbox control to from using Add() method.
// Add this textbox to form this.Controls.Add(Mytextbox);
Example:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacemy {publicpartialclassForm1 : Form {publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender, EventArgs e){// Creating and setting the properties of Lable1Label Mylablel =newLabel();Mylablel.Location =newPoint(96, 54);Mylablel.Text ="Enter Name";Mylablel.AutoSize =true;Mylablel.BackColor = Color.LightGray;// Add this label to formthis.Controls.Add(Mylablel);// Creating and setting the properties of TextBox1TextBox Mytextbox =newTextBox();Mytextbox.Location =newPoint(187, 51);Mytextbox.BackColor = Color.LightGray;Mytextbox.ForeColor = Color.DarkOliveGreen;Mytextbox.AutoSize =true;Mytextbox.Name ="text_box1";// Add this textbox to formthis.Controls.Add(Mytextbox);}}}chevron_rightfilter_noneOutput:

- How to set the Margin between the TextBox controls in C#?
- How to set the Margin between the CheckBox controls in C#?
- How to set the Text in TextBox in C#?
- How to set the PasswordChar of the TextBox in C#?
- How to set the Location of the TextBox in C#?
- How to set the Visibility of the TextBox in C#?
- How to set Scrollbar in TextBox in C#?
- How to provide name to the TextBox in C#?
- How to set the Length of the Characters in TextBox in C#?
- How to set the font of the TextBox Content in C#?
- How to set the background color of the TextBox in C#?
- How to set the foreground color of the TextBox in C#?
- How to set the Alignment of the Text in the TextBox in C#?
- How to create Multiline TextBox in C#?
- How to use AcceptsReturn Property of TextBox in C#?
- How to change the TextBox Border Style in C#?
- How to set Default Password Character in TextBox in C#?
- How to change the case of the characters present in the TextBox in C#?
Important properties of TextBox
| Property | Description |
|---|---|
| AcceptsReturn | This property is used to set a value which shows whether pressing ENTER in a multiline TextBox control creates a new line of text in the control or activates the default button for the given form. |
| AutoSize | This property is used to adjust the size of the TextBox according to the content. |
| BackColor | This property is used to set the background color of the TextBox. |
| BorderStyle | This property is used to adjust the border type of the textbox. |
| CharacterCasing | This property is used to check whether the TextBox control modifies the case of characters as they are typed. |
| Events | This property is used to provide a list of event handlers that are attached to this Component. |
| Font | This property is used to adjust the font of the text displayed by the textbox control. |
| ForeColor | This property is used to adjust the foreground color of the textbox control. |
| Location | This property is used to adjust the coordinates of the upper-left corner of the textbox control relative to the upper-left corner of its form. |
| Margin | This property is used to set the margin between two textbox controls. |
| MaxLength | This property is used to set the maximum number of characters the user can type or paste into the text box control. |
| Multiline | This property is used to set a value which shows whether this is a multiline TextBox control. |
| Name | This property is used to provide a name to the TextBox control. |
| PasswordChar | This property is used to set the character used to mask characters of a password in a single-line TextBox control. |
| ScrollBars | This property is used to set which scroll bars should appear in a multiline TextBox control. |
| Text | This property is used to set the text associated with this control. |
| TextAlign | This property is used to adjust the alignment of the text in the TextBox control. |
| TextLength | This property is used to get the length of the text in the TextBox control. |
| UseSystemPasswordChar | This property is used to set a value which shows whether the text in the TextBox control should appear as the default password character. |
| Visible | This property is used to get or set a value which determine whether the control and all its child controls are displayed. |
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

