This documentation is archived and is not being maintained.

TextBox Class

Represents a Windows text box control.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public class TextBox : TextBoxBase

With the TextBox control, the user can enter text in an application. This control has additional functionality that is not found in the standard Windows text box control, including multiline editing and password character masking.

Typically, a TextBox control is used to display, or accept as input, a single line of text. You can use the Multiline and ScrollBars properties to enable multiple lines of text to be displayed or entered. Set the AcceptsTab and AcceptsReturn properties to true to enable greater text manipulation in a multiline TextBox control.


You must set the Multiline property to true to adjust the height of the TextBox control. You can adjust the height by setting the Size property.

You can limit the amount of text entered into a TextBox control by setting the MaxLength property to a specific number of characters. TextBox controls can also be used to accept passwords and other sensitive information. You can use the PasswordChar property to mask characters entered in a single-line version of the control. Use the CharacterCasing property to enable the user to type only uppercase, only lowercase, or a combination of uppercase and lowercase characters into the TextBox control.

To scroll the contents of the TextBox until the cursor (caret) is within the visible region of the control, you can use the ScrollToCaret method. To select a range of text in the text box, you can use the Select method.

To restrict text from being entered in a TextBox control, you can create an event handler for the KeyDown event in order to validate each character entered in the control. You can also restrict all entry of data in a TextBox control by setting the ReadOnly property to true.


Most of the functionality of the TextBox control is inherited from the TextBoxBase class.

Using the TextBox control with visual styles enabled will cause the incorrect handling of surrogate fonts.

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: In Pocket PC applications, tabs in a single-line text box display as brackets, but display in the usual way when Multiline is set to true.

The following code example creates a multiline TextBox control with vertical scroll bars. This example uses the AcceptsTab, AcceptsReturn, and Dock properties to make the multiline text box control useful for creating text documents.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
    private TextBox textBox1;

    public Form1()

    private void InitializeComponent()
        this.textBox1 = new System.Windows.Forms.TextBox();
        // textBox1 
        this.textBox1.AcceptsReturn = true;
        this.textBox1.AcceptsTab = true;
        this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.textBox1.Multiline = true;
        this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
        // Form1 
        this.ClientSize = new System.Drawing.Size(284, 264);
        this.Text = "TextBox Example";


    static void Main()
        Application.Run(new Form1());

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0