Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace: http://schemas.microsoft.com/winfx/2006/xaml/presentation
'Declaration <ContentPropertyAttribute("Text")> _ <LocalizabilityAttribute(LocalizationCategory.Text)> _ Public Class TextBox Inherits TextBoxBase Implements IAddChild 'Usage Dim instance As TextBox
/** @attribute ContentPropertyAttribute("Text") */ /** @attribute LocalizabilityAttribute(LocalizationCategory.Text) */ public class TextBox extends TextBoxBase implements IAddChild
ContentPropertyAttribute("Text") LocalizabilityAttribute(LocalizationCategory.Text) public class TextBox extends TextBoxBase implements IAddChild
The following graphic shows an example of a TextBox.
Example of a TextBox
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 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.
This example shows one way to use the SelectedText property to retrieve text that the user has selected in a TextBox control.
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.
Sub OnClick(ByVal senter As Object, ByVal e As RoutedEventArgs) Dim sSelectedText As String = tbSelectSomeText.SelectedText End Sub
|How to: Set the Text Content of a TextBox Control|| |
This example shows how to use the Text property to set the initial text contents of a TextBox control.
|How to: Set Focus in a TextBox Control|| |
This example shows how to use the Focus method to set focus on a TextBox control.
|How to: Detect When Text in a TextBox Has Changed|| |
This example shows one way to use the TextChanged event to execute a method whenever the text in a TextBox control has changed.
|How to: Position the Cursor at the Beginning or End of Text in a TextBox Control|| |
This example shows how to position the cursor at the beginning or end of the text contents of a TextBox control.
|How to: Use a Custom Context Menu with a TextBox|| |
This example shows how to define and implement a simple custom context menu for a TextBox.
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter EditionThe Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.