Updated: February 2009
Represents a control that can be used to display or edit unformatted text.
Namespace:
System.Windows.Controls
Assembly:
PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Visual Basic (Declaration)
<ContentPropertyAttribute("Text")> _
<LocalizabilityAttribute(LocalizationCategory.Text)> _
Public Class TextBox _
Inherits TextBoxBase _
Implements IAddChild
[ContentPropertyAttribute("Text")]
[LocalizabilityAttribute(LocalizationCategory.Text)]
public class TextBox : TextBoxBase, IAddChild
[ContentPropertyAttribute(L"Text")]
[LocalizabilityAttribute(LocalizationCategory::Text)]
public ref class TextBox : public TextBoxBase,
IAddChild
public class TextBox extends TextBoxBase implements IAddChild
XAML Object Element Usage
<TextBox>
Text
</TextBox>
Content Model: A TextBox can contain only unformatted text. See TextBox Content Model Overview for more information.
The following graphic shows an example of a TextBox.
Example of a TextBox
.gif)
TextBox is a composite control that that is composed of several encapsulated components. Consequently, some events do not bubble up to the containing control because they are handled by encapsulated child elements. Because of this, application developers should listen for the tunneling version of an event (denoted by the prefix "Preview").
TextBox supports unformatted text only. For applications that require support for richer content, see RichTextBox. For applications that need to accept passwords or other sensitive input, see PasswordBox.
Horizontally and vertically aligning text within a TextBox is done with the HorizontalContentAlignment and VerticalContentAlignment properties. Aligning the TextBox within the layout of the page is done with the HorizontalAlignment and VerticalAlignment properties.
The best way to hide the border around a TextBox is to set the BorderThickness property of the TextBox to 0.
Important Note: |
|---|
TextBox has built-in handling for the bubbling MouseUp and MouseDown events. Consequently, custom event handlers that listen for MouseUp or MouseDown events from a TextBox will not be called. If you need to respond to these events, listen for the tunneling PreviewMouseUp and PreviewMouseDown events instead, or register the handlers with the HandledEventsToo argument (this latter option is only available through code). Do not mark the event handled unless you deliberately want to disable TextBox native handling of these events, and be aware that this has notable effects on the control's UI. |
Scrollbars are not visible on a TextBox by default. To make scrollbars visible, set the VerticalScrollBarVisibility and HorizontalScrollBarVisibility properties to Visible or Auto.
Usually the TextChanged event should be used to detect whenever the text in a TextBox or RichTextBox changes rather then KeyDown as you might expect. See How to: Detect When Text in a TextBox Has Changed for an example.
Dependency properties for this control might be set by the control’s default style. If a property is set by a default style, the property might change from its default value when the control appears in the application. The default style is determined by which desktop theme is used when the application is running. For more information, see Themes.
This example shows one way to use the SelectedText property to retrieve text that the user has selected in a TextBox control.
The following Extensible Application Markup Language (XAML) example shows the definition of a TextBox control that contains some text to select, and a Button control with a specified OnClick method.
In this example, a button with an associated Click event handler is used to retrieve the text selection. When the user clicks the button, the OnClick method copies any selected text in the textbox into a string. The particular circumstances by which the text selection is retrieved (clicking a button), as well as the action taken with that selection (copying the text selection to a string), can easily be modified to accommodate a wide variety of scenarios.
<TextBox Name="tbSelectSomeText">
Some text to select...
</TextBox>
<Button Click="OnClick">Retrieve Selection</Button>
The following C# example shows an OnClick event handler for the button defined in the XAML for this example.
Private Sub OnClick(ByVal senter As Object, ByVal e As RoutedEventArgs)
Dim sSelectedText As String = tbSelectSomeText.SelectedText
End Sub
void OnClick(object sender, RoutedEventArgs e)
{
String sSelectedText = tbSelectSomeText.SelectedText;
}
More Code
System..::.Object
System.Windows.Threading..::.DispatcherObject
System.Windows..::.DependencyObject
System.Windows.Media..::.Visual
System.Windows..::.UIElement
System.Windows..::.FrameworkElement
System.Windows.Controls..::.Control
System.Windows.Controls.Primitives..::.TextBoxBase
System.Windows.Controls..::.TextBox
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 Server 2008 R2, Windows Server 2008, Windows Server 2003
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
Reference
Other Resources
Date | History | Reason |
|---|
February 2009
| Described how default styles change dependency properties. |
Customer feedback.
|