Open In App

C# TextBox Controls

Last Updated : 25 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Empth-forms

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.

ToolBox


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

TextBox


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.

Properties


Output:

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:

Output


Properties

PropertyDescription
TextThis property is used to set the text inside the TextBox.
TextAlignIt is used to Set or get the alignment of the text inside the TextBox for example left, centre, and right.
MaxLengthUsed to set or get the maximum number of characters the user can type into the TextBox.
MultilineThis 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.
PasswordCharIt is used to set or get the character used to hide text used where showing the password characters.
ReadOnlyThis property is used to set or get whether the TextBox is read-only (users can’t edit the text).
WordWrapSets or gets whether the text automatically moves to the next line when it’s too long for the box (only for multiline).
ScrollBarsSets or gets which type of scroll bars (horizontal, vertical, both, or none) appear in the TextBox.
DockSets or gets how the TextBox is positioned in its container (e.g., fill, top, left).
AnchorSets or gets which edges of the parent container the TextBox should stay anchored to when resized.
BorderStyleSets or gets the style of the border around the TextBox (e.g., solid line, 3D, no border).
EnabledSets or gets whether the TextBox is enabled (if false, users can’t interact with it).
FocusedThis is the commonly used property to check if the TextBox currently has focus if it is selected.
FontSets or gets the font used for the text (like size and style).
ForeColorSets or gets the colour of the text inside the TextBox.
BackColorSets or gets the background colour of the TextBox.
AcceptsReturnSets whether pressing the Enter key adds a new line (used for multi-line TextBox).
HideSelectionSets whether selected text is hidden when the TextBox loses focus.
SelectionStartIt is used to set from where the selected text starts in the TextBox.
SelectionLengthThis property gets or sets the length of the selected text.
ShortcutsEnabledIt 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.


Next Article

Similar Reads