Last Updated : 25 Mar, 2025
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 FormsThere are mainly two ways to create a TextBox in Windows forms which are mentioned below.
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 (iffalse
, 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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4