Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Last Updated: Mar 27, 2024

C# TextBox Control

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM


A textBox is a crucial component of Windows forms. The user can enter data into the application using TextBox, which can be a single line or many lines. Actually, the class name is TextBox, and its definition is present under the namespace System.Windows.Forms. In C#, TextBox can be created by two different methods:

  1. Design-Time
  2. Run-Time

Let us see these two methods in detail with implementation.


This one is the easiest way to make a RichTextBox. 

Following are the steps to do it.

  • The first step will be to create a form window, as shown in the following image:
  • In Visual Studio, you will have to click on the File menu, then go to New. After that, click on Project, then WindowsFormApp.

  • We will drag and drop the TextBox Control available in the toolbox to the form in the second step. 

The TextBox can be placed anywhere on the form as per requirements.

  • After successful drag and drop, the properties of the TextBox control can be modified easily as per your requirements. 


In this method, you can create a TextBox control using a program with the help of syntax provided by the TextBox class.

Following are the steps to create a TextBox control dynamically;

The TextBox class provides the TextBox() constructor for creating a TextBox control.

  • The first step is to create a textbox with the help of the TextBox() constructor.


// Creating textbox
TextBox Mytextbox = new TextBox();


  • After successfully creating the RichBox control, in the second step, we have to set the property as per our requirement of the TextBox control 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";


  • The last step is to add the created RichTextBox control to the form. This can be done by:


// Add this textbox to form


Let us see an example:


using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using System.Threading.Tasks;
namespace my {
public partial class MyForm: Form {
    public MyForm() {
    private void MyForm_Load(object sender, EventArgs e)
        // Let's create Lable1 and setting its properties
        Label Mylablel = new Label();
        Mylablel.Location = new Point(96, 54);
        Mylablel.Text = "Enter Name";
        Mylablel.AutoSize = true;
        Mylablel.BackColor = Color.LightGray;
        // Add this label to form
        // Creating and setting the properties of TextBox1
        TextBox Mytextbox = new TextBox();
        Mytextbox.Location = new Point(187, 51);
        Mytextbox.BackColor = Color.LightGray;
        Mytextbox.ForeColor = Color.DarkOliveGreen;
        Mytextbox.AutoSize = true;
        Mytextbox.Name = "text_box1";
        // Add this textbox to form



Properties of TextBox 

Some of the essential properties are:

  • AutoSize: It is used to adjust the size of the TextBox according to the content.
  • BorderStyle: It is used to adjust the border type of the textbox.
  • BackColor: It is used to set the color of the background of the TextBox.
  • Events: It is used to provide a list of event handlers that are attached to this Component.
  • Location: It is used to modify the coordinates of the upper-left corner of the textbox relative to the upper-left corner of its form.
  • Margin: It is used to set the margin between two textbox controls.
  • Visible: It is used to get or set a value that determines whether the control and all its child controls are displayed.

Recommended topics, Palindrome in C#, singleton design pattern in c# and Ienumerable vs Iqueryable

Frequently Asked Questions

What is TextBox control?

Text box controls are used for entering text at runtime on a form. A single line of text is taken in by default. But, you can change the settings to accept multiple texts. Also, we can even add scroll bars to it. 

What is the difference between TextBox and RichTextBox?

The RichTextBox is similar to the TextBox, but it has additional formatting capabilities. Whereas the TextBox control allows the Font property to be set for the entire control, the RichTextBox will enable you to select the Font and other formatting properties for selections within the text displayed in control.

What are the properties of TextBox control?

TextBox Properties

  • TextAlign– for setting text alignment.
  • ScrollBars– for adding scrollbars, both vertical and horizontal.
  • Multiline– to set the TextBox Control to allow multiple lines.
  • MaxLength– for specifying the maximum character number the TextBox Control will accept.
  • Index– for specifying the index of the control array.

What is the difference between text and label?

A Text is like a geometric shape (like a Rectangle or a Circle), while the label is a control based on UI (like a Button or a CheckBox). In Swing, geometric shapes were restricted to the painting mechanism, while in JavaFX, they can be used in more generic ways.

What is TextBox control with an example?

A TextBox control is used to accept or display, as input, a single line of text. VB.Net programmers use TextBox control extensively to let the user view or enter a large amount of text. A text box object is generally used to display text on a form or to get user input while a VB.Net program is running.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job


In this article, we have studied about TextBox in C#. We have also discussed the properties of TextBox and different events in detail.

We hope that this article has provided you with the help to enhance your knowledge regarding TextBox in C# with different ways to create it and if you would like to learn more, check out our articles on C# GenericsC# Variables and C# Reflection.

Read also, .Net.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available, Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Topics covered
Properties of TextBox 
Frequently Asked Questions
What is TextBox control?
What is the difference between TextBox and RichTextBox?
What are the properties of TextBox control?
What is the difference between text and label?
What is TextBox control with an example?