System.Windows.Controls Nam ...


.NET Framework Class Library for Silverlight
TextBox Class

Represents a control that can be used to display single-format, multi-line text.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)
Syntax

Visual Basic (Declaration)
<TemplateVisualStateAttribute(Name := "MouseOver", GroupName := "CommonStates")> _
<TemplatePartAttribute(Name := "ContentElement", Type := GetType(FrameworkElement))> _
<TemplateVisualStateAttribute(Name := "Normal", GroupName := "CommonStates")> _
<TemplateVisualStateAttribute(Name := "Disabled", GroupName := "CommonStates")> _
<TemplateVisualStateAttribute(Name := "ReadOnly", GroupName := "CommonStates")> _
<TemplateVisualStateAttribute(Name := "InvalidUnfocused", GroupName := "ValidationStates")> _
<TemplateVisualStateAttribute(Name := "Unfocused", GroupName := "FocusStates")> _
<TemplateVisualStateAttribute(Name := "Focused", GroupName := "FocusStates")> _
<TemplateVisualStateAttribute(Name := "Valid", GroupName := "ValidationStates")> _
<TemplateVisualStateAttribute(Name := "InvalidFocused", GroupName := "ValidationStates")> _
Public Class TextBox _
    Inherits Control
Visual Basic (Usage)
Dim instance As TextBox
C#
[TemplateVisualStateAttribute(Name = "MouseOver", GroupName = "CommonStates")]
[TemplatePartAttribute(Name = "ContentElement", Type = typeof(FrameworkElement))]
[TemplateVisualStateAttribute(Name = "Normal", GroupName = "CommonStates")]
[TemplateVisualStateAttribute(Name = "Disabled", GroupName = "CommonStates")]
[TemplateVisualStateAttribute(Name = "ReadOnly", GroupName = "CommonStates")]
[TemplateVisualStateAttribute(Name = "InvalidUnfocused", GroupName = "ValidationStates")]
[TemplateVisualStateAttribute(Name = "Unfocused", GroupName = "FocusStates")]
[TemplateVisualStateAttribute(Name = "Focused", GroupName = "FocusStates")]
[TemplateVisualStateAttribute(Name = "Valid", GroupName = "ValidationStates")]
[TemplateVisualStateAttribute(Name = "InvalidFocused", GroupName = "ValidationStates")]
public class TextBox : Control
XAML Object Element Usage
<TextBox .../>
Remarks

You can enable multi-line text in a TextBox control by using the AcceptsReturn property. Use the HorizontalScrollBarVisibility or VerticalScrollBarVisibility property to enable the horizontal scrollbars or the vertical scrollbars. For an example that demonstrates a multi-line text box control with scrollbars, see the AcceptsReturn property.

You may want to use a text box to display text, but you may not want the user to be able to edit the text. You can do this by using the IsReadOnly property. If you set the IsReadOnly property of a text box to true, edit commands are not supported and the KeyUp and KeyDown events are marked as handled. If you require formatted text that can not be edited by the user, use the TextBlock control. If you require an editable text box that accepts passwords or other sensitive input, see PasswordBox.

You can modify the border of the TextBox by using the BorderThickness and BorderBrush properties. The best way to hide the border around a TextBox is to set the BorderThickness property of the TextBox to 0.

Content Model: The content property of the TextBox is Text. For more information about the content model for a text box, see Control Content Models.

Customizing the TextBox Control

To apply the same property settings to multiple TextBox controls, use the Style property. To change the visual structure and visual behavior of a TextBox, copy and modify its default style and template. For more information, see Control Customization.

If a dependency property for a TextBox is set by its default style, the property might change from its default value when the TextBox appears in the application. For more information, see Dependency Property Value Precedence. You can get the default style and template for TextBox from TextBox Styles and Templates.

NoteNote:

Setting a visual property will only have an effect if that property is both present in TextBox control's default template and is set by using a TemplateBinding. You can find a list of visual properties in the "Changing the Visual Structure of a Control" section in Customizing the Appearance of an Existing Control by Using a ControlTemplate.

Examples

The following code example shows text box controls that demonstrate various properties and events of a TextBox object.

Run this sample

XAML
<StackPanel x:Name="LayoutRoot" Background="White">

    <TextBlock Margin="20,20,0,0" Text="Type Text Here"/>

    <TextBox x:Name="ReadWriteTB" TextChanged="ReadWriteTB_TextChanged"  IsReadOnly="False" HorizontalAlignment="Left" Margin="20,5,0,0" Height="35" Width="200" />

    <TextBlock Margin="20,20,0,0" Text="Read Only TextBox"/>

    <TextBox x:Name="ReadOnlyTB"  IsReadOnly="True" HorizontalAlignment="Left" Margin="20,5,0,0" Height="35" Width="200" />

    <TextBlock Margin="20,30,0,0" Text="Search Type TextBox" />

    <TextBlock Margin="20,0,0,0" FontSize="11">
        The following text box has a watermark text (Search). The text is gray until you click inside the text box. 
        <LineBreak />
        When you click inside the text box the watermark text is removed and the cursor appears ready for input.
    </TextBlock>

    <TextBox x:Name="SearchTB" Margin="20,5,0,0" Text="Search" HorizontalAlignment="Left" Height="35" Width="200" Foreground="Gray" GotFocus="SearchTB_GotFocus" LostFocus="SearchTB_LostFocus" />

    <TextBlock Margin="20,40,0,0" Text="Demonstrating styles for TextBox" />

    <TextBlock Margin="20,0,0,0" FontSize="11">
        Select the following text to view the results of SelectionForeground and SelectionBackground properties.
    </TextBlock>

    <TextBox Text="Styles" Margin="20,5,0,0" HorizontalAlignment="Left" Width="200" Height="35" FontFamily="Arial" FontSize="15" Foreground="White" Background="Black" BorderBrush="White" SelectionForeground="Black">
        <TextBox.SelectionBackground>
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                <GradientStop Color="Gray" Offset="0.0" />
                <GradientStop Color="White" Offset="1.0" />
            </LinearGradientBrush>
        </TextBox.SelectionBackground>
    </TextBox>
</StackPanel>
Visual Basic
Public Class Page
    Inherits UserControl

    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub

    'The following method displays the text entered in ReadWriteTB in ReadOnlyTB.
    Private Sub ReadWriteTB_TextChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
        ReadOnlyTB.Text = ReadWriteTB.Text
    End Sub

    'The foreground color of the text in SearchTB is set to Magenta when SearchTB
    'gets focus.
    Private Sub SearchTB_GotFocus(ByVal sender As Object, ByVal e As RoutedEventArgs)
        SearchTB.Text = ""
        Dim Brush1 As SolidColorBrush = New SolidColorBrush
        Brush1.Color = Colors.Magenta
        SearchTB.Foreground = Brush1
    End Sub

    'The foreground color of the text in SearchTB is set to Blue when SearchTB
    'loses focus. Also, if SearchTB loses focus and no text is entered, the
    'text "Search" is displayed.
    Private Sub SearchTB_LostFocus(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If (SearchTB.Text = String.Empty) Then
            SearchTB.Text = "Search"
            Dim Brush2 As SolidColorBrush = New SolidColorBrush
            Brush2.Color = Colors.Blue
            SearchTB.Foreground = Brush2
        End If
    End Sub
End Class
C#
public partial class Page : UserControl
{
    public Page()
    {
        InitializeComponent();
    }


    //The following method displays the text entered in ReadWriteTB in ReadOnlyTB.
    private void ReadWriteTB_TextChanged(object sender, RoutedEventArgs e)
    {
        ReadOnlyTB.Text = ReadWriteTB.Text;
    }

    //The foreground color of the text in SearchTB is set to Magenta when SearchTB
    //gets focus.
    private void SearchTB_GotFocus(object sender, RoutedEventArgs e)
    {
        SearchTB.Text = "";
        SolidColorBrush Brush1 = new SolidColorBrush();
        Brush1.Color = Colors.Magenta;
        SearchTB.Foreground = Brush1;

    }

    //The foreground color of the text in SearchTB is set to Blue when SearchTB
    //loses focus. Also, if SearchTB loses focus and no text is entered, the
    //text "Search" is displayed.
    private void SearchTB_LostFocus(object sender, RoutedEventArgs e)
    {
        if (SearchTB.Text == String.Empty)
        {
            SearchTB.Text = "Search";
            SolidColorBrush Brush2 = new SolidColorBrush();
            Brush2.Color = Colors.Blue;
            SearchTB.Foreground = Brush2;
        }
    }
}
Inheritance Hierarchy

System..::.Object
  System.Windows..::.DependencyObject
    System.Windows..::.UIElement
      System.Windows..::.FrameworkElement
        System.Windows.Controls..::.Control
          System.Windows.Controls..::.TextBox
            System.Windows.Controls.Primitives..::.DatePickerTextBox
Thread Safety

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

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference

Other Resources

Tags :


Page view tracker