Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
How to create Multiline TextBox in C#?
Next article icon

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
How to create Multiline TextBox in C#?
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp-Windows-Forms-Namespace

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 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 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 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences