Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The TextBox class enables you to display or edit unformatted text. A common use of a TextBox is editing unformatted text in a form. For example, a form asking for the user's name, phone number, and other information would use TextBox controls for text input.
The following table lists common tasks for working with the TextBox control:
| Title | Description |
|---|---|
| Create a Multiline TextBox Control | Learn how to create a TextBox that accepts multiple lines of text. |
| Detect When Text in a TextBox Has Changed | Learn how to respond when text changes in a TextBox. |
| Enable Tab Characters in a TextBox Control | Learn how to allow tab characters in a TextBox. |
| Get a Collection of Lines from a TextBox | Learn how to retrieve lines of text from a TextBox. |
| Make a TextBox Control Read-Only | Learn how to prevent editing in a TextBox. |
| Position the Cursor at the Beginning or End of Text in a TextBox Control | Learn how to set the cursor position in a TextBox. |
| Retrieve a Text Selection | Learn how to get selected text from a TextBox. |
| Set Focus in a TextBox Control | Learn how to give focus to a TextBox. |
| Set the Text Content of a TextBox Control | Learn how to set the initial text in a TextBox. |
| Enable Spell Checking in a Text Editing Control | Learn how to enable spell checking in a TextBox. |
| Use a Custom Context Menu with a TextBox | Learn how to create a custom context menu for a TextBox. |
| Use Spell Checking with a Context Menu | Learn how to integrate spell checking with a context menu. |
| Add a Watermark to a TextBox | Learn how to display placeholder text in a TextBox. |
TextBox or RichTextBox?
Both TextBox and RichTextBox allow users to input text, but the two controls are used for different scenarios. A TextBox requires less system resources than a RichTextBox, so it's ideal when only plain text needs to be edited (that is, usage in a form). A RichTextBox is a better choice when it's necessary for the user to edit formatted text, images, tables, or other supported content. For example, editing a document, article, or blog that requires formatting, images, and other content is best accomplished using a RichTextBox. The following table summarizes the primary features of TextBox and RichTextBox.
| Control | Real-time Spellchecking | Context Menu | Formatting commands like ToggleBold (Ctr+B) | FlowDocument content like images, paragraphs, tables, and others |
|---|---|---|---|---|
| TextBox | Yes | Yes | No | No. |
| RichTextBox | Yes | Yes | Yes (see RichTextBox Overview) | Yes (see RichTextBox Overview) |
Note
Although TextBox doesn't support formatting related editing commands like ToggleBold (Ctr+B), many basic commands are supported by both controls such as MoveToLineEnd. For more information, see EditingCommands.
Features supported by TextBox are covered in the sections below. For more information about RichTextBox, see RichTextBox Overview.
Real-time spellchecking
You can enable real-time spellchecking in a TextBox or RichTextBox. When spellchecking is turned on, a red line appears underneath any misspelled words (see the following picture).
To learn how to enable spellchecking, see Enable Spell Checking in a Text Editing Control.
Context menu
By default, both TextBox and RichTextBox have a context menu that appears when a user right-clicks inside the control. The context menu allows the user to cut, copy, or paste (see the following picture).
You can create your own custom context menu to override the default behavior. For more information, see Use a Custom Context Menu with a TextBox.
Creating TextBoxes
A TextBox can be a single line in height or comprise multiple lines. A single line TextBox is best for inputting small amounts of plain text (for example, "Name", "Phone Number", and other information in a form). The following example shows how to create a single line TextBox.
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<TextBox Width="200" MaxLength="100" />
</StackPanel>
</Page>
You can also create a TextBox that allows the user to enter multiple lines of text. For example, if your form asked for a biographical sketch of the user, you would want to use a TextBox that supports multiple lines of text. The following example shows how to use Extensible Application Markup Language (XAML) to define a TextBox control that automatically expands to accommodate multiple lines of text.
<TextBox
Name="tbMultiLine"
TextWrapping="Wrap"
AcceptsReturn="True"
VerticalScrollBarVisibility="Visible"
>
This TextBox will allow the user to enter multiple lines of text. When the RETURN key is pressed,
or when typed text reaches the edge of the text box, a new line is automatically inserted.
</TextBox>
Setting the TextWrapping attribute to Wrap causes text to wrap to a new line when the edge of the TextBox control is reached, automatically expanding the TextBox control to include room for a new line, if necessary.
Setting the AcceptsReturn attribute to true causes a new line to be inserted when the RETURN key is pressed, once again automatically expanding the TextBox to include room for a new line, if necessary.
The VerticalScrollBarVisibility attribute adds a scroll bar to the TextBox, so that the contents of the TextBox can be scrolled through if the TextBox expands beyond the size of the frame or window that encloses it.
For more information on different tasks associated with using a TextBox, see the how-to topics listed at the top of this article.
Detect when content changes
Usually the TextChanged event should be used to detect whenever the text in a TextBox or RichTextBox changes, rather than KeyDown as you might expect. For an example, see Detect When Text in a TextBox Has Changed.
Styles and templates
You can modify the default ControlTemplate to give the TextBox control a unique appearance. For more information, see What are styles and templates? and How to create a template for a control.
Content property
The TextBox control uses the Text property to display the text content.
Parts
The following table lists the named parts for the TextBox control.
| Part | Type | Description |
|---|---|---|
| PART_ContentHost | FrameworkElement | The framework element that hosts the text content. |
Visual states
The following table lists the visual states for the TextBox control.
| VisualState Name | VisualStateGroup Name | Description |
|---|---|---|
| Disabled | CommonStates | The control is disabled. |
| Focused | FocusStates | The control has keyboard focus. |
| InvalidFocused | ValidationStates | The control has a validation error and has keyboard focus. |
| InvalidUnfocused | ValidationStates | The control has a validation error but doesn't have keyboard focus. |
| MouseOver | CommonStates | The mouse is over the control. |
| Normal | CommonStates | The control is in its normal state. |
| ReadOnly | CommonStates | The control is in read-only mode. |
| Unfocused | FocusStates | The control doesn't have keyboard focus. |
| Valid | ValidationStates | The control is valid and has no validation errors. |
See also
.NET Desktop feedback