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 multiple lines. The TextBox is a class and it is defined under System.Windows.Forms namespace.
Ways to Create a TextBox In Windows Forms
There are mainly two ways to create a TextBox in Windows forms which are mentioned below.
- Drag and drop (Design-Time)
- Custom TextBox (Run-Time)
Drag and drop (Design-Time)
This is the easiest way to create a TextBox in Windows Forms using Visual Studio we just have to open the toolbox and drag and drop the text box on the form in the designer and further we can change the appearance of the TextBox using the properties. Follow these steps to create a TextBox.
Step 1: Now locate the project with the name here we are using the default name which is Form1 and it will open a form in the editor that we can further modify.

In the image, we have two files that are open one Design and there is Form1.cs these two play a major role. We use the Form 1.cs file for the custom logic.
Step 2: Now open a Toolbox go to the view > Toolbox or ctrl + alt + x.

Step 3. Now open the common controls and drag and drop the TextBox on the form where we want to it be placed.

Step 4. Now open the properties of the TextBox press right-click on the TextBox and it will open the properties solution explorer now we can change the button appearance of the textbox and also add different properties like default text.

Output:

Custom TextBox (Run Time)
In this method, we are going to modify the Form1.cs file and add custom code modification in C# with the help of the TextBox class. The following steps show how to create a TextBox dynamically:
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 form using Add() method.
// Add this textbox to form
this.Controls.Add(Mytextbox);
Step 4: Now double-click on the form in Design and it will open the Form1.cs file where code is written in C#. Here the program file is Form 1.cs Now write this code in Form1.cs file
Form1.cs file:
C#
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Creating and setting the properties of Label
Label myLabel = new Label();
myLabel.Location = new Point(96, 54);
myLabel.Text = "Enter Your Name";
myLabel.AutoSize = true;
myLabel.BackColor = Color.LightGray;
// Add this label to form
this.Controls.Add(myLabel);
// Creating and setting the properties of TextBox
TextBox myTextBox = new TextBox();
myTextBox.Location = new Point(187, 51);
myTextBox.BackColor = Color.LightGray;
myTextBox.ForeColor = Color.DarkOliveGreen;
myTextBox.AutoSize = true;
myTextBox.Name = "textBox1";
// Add this textbox to form
this.Controls.Add(myTextBox);
}
}
}
Output:

Properties
Property | Description |
---|
Text | This property is used to set the text inside the TextBox. |
TextAlign | It is used to Set or get the alignment of the text inside the TextBox for example left, centre, and right. |
MaxLength | Used to set or get the maximum number of characters the user can type into the TextBox. |
Multiline | This is used to set or determine whether the TextBox allows multiple lines of text. If press enter it allows you to write in the next line. |
PasswordChar | It is used to set or get the character used to hide text used where showing the password characters. |
ReadOnly | This property is used to set or get whether the TextBox is read-only (users can’t edit the text). |
WordWrap | Sets or gets whether the text automatically moves to the next line when it’s too long for the box (only for multiline). |
ScrollBars | Sets or gets which type of scroll bars (horizontal, vertical, both, or none) appear in the TextBox. |
Dock | Sets or gets how the TextBox is positioned in its container (e.g., fill, top, left). |
Anchor | Sets or gets which edges of the parent container the TextBox should stay anchored to when resized. |
BorderStyle | Sets or gets the style of the border around the TextBox (e.g., solid line, 3D, no border). |
Enabled | Sets or gets whether the TextBox is enabled (if false , users can’t interact with it). |
Focused | This is the commonly used property to check if the TextBox currently has focus if it is selected. |
Font | Sets or gets the font used for the text (like size and style). |
ForeColor | Sets or gets the colour of the text inside the TextBox. |
BackColor | Sets or gets the background colour of the TextBox. |
AcceptsReturn | Sets whether pressing the Enter key adds a new line (used for multi-line TextBox). |
HideSelection | Sets whether selected text is hidden when the TextBox loses focus. |
SelectionStart | It is used to set from where the selected text starts in the TextBox. |
SelectionLength | This property gets or sets the length of the selected text. |
ShortcutsEnabled | It sets the keyboard shortcuts such as copy (Ctrl + C) or paste (Ctrl + P). |
Clear() | This property is used to clear the text inside the TextBox. |
SelectAll() | We can use this property to select all the text inside the TextBox. |
Similar Reads
C# | RichTextBox Class
In C#, RichTextBox control is a textbox which gives you rich text editing controls and advanced formatting features also includes a loading rich text format (RTF) files. Or in other words, RichTextBox controls allows you to display or edit flow content, including paragraphs, images, tables, etc. The
5 min read
C# | MaskedTextBox Class
In C#, MaskedTextBox control gives a validation procedure for the user input on the form like date, phone numbers, etc. Or in other words, it is used to provide a mask which differentiates between proper and improper user input. The MaskedTextBox class is used to represent the windows masked text bo
6 min read
C# | ListBox Class
In Windows Forms, ListBox control is used to show multiple elements in a list, from which a user can select one or more elements and the elements are generally displayed in multiple columns. The ListBox class is used to represent the windows list box and also provide different types of properties, m
5 min read
How to set Scrollbar in TextBox in C#?
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. In TextBox, you are allowed to set scrollbars when you are working with multiline TextBox with the help of ScrollBars property of
3 min read
How to create Multiline TextBox in C#?
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. In TextBox, you are allowed to create a multiline TextBox which stores multiple lines of the content using Multiline property of t
3 min read
C# | GroupBox Class
In Windows form, GroupBox is a container which contains multiple controls on it and the controls are related to each other. Or in other words, GroupBox is a frame display around a group of controls with a suitable optional title. Or a GroupBox is used to categorize the related controls in a group. T
5 min read
ComboBox in C#
In Windows Forms, the ComboBox control combines the features of a TextBox and a ListBox. It displays one item at a time, with additional items accessible through a drop-down menu. The ComboBox class is part of the System.Windows.Forms namespace. Ways to Create a ComboBox In Windows FormsThere are ma
5 min read
How to set the Location of the TextBox in C#?
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. In TextBox, you are allowed to set the coordinates of the upper-left corner of the TextBox control relative to the upper-left corn
3 min read
How to set the Text in TextBox in C#?
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. In TextBox, you are allowed to set the text associated with the TextBox by using the Text property of the TextBox. In Windows form
3 min read
How to provide name to the TextBox in C#?
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. In TextBox, you are allowed to assign a name to the TextBox control with the help of Name property of the TextBox. The default val
3 min read