Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

TextBox.MaxLength Property

Gets or sets the value that determines the maximum number of characters allowed for user input.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)
'Declaration
Public Property MaxLength As Integer
<TextBox MaxLength="int"/>

Property Value

Type: System.Int32
The maximum number of characters allowed for user input. The default is 0.
ExceptionCondition
ArgumentOutOfRangeException

The value set is less than 0.

ArgumentNullException

The value set is Nothing.

Dependency property identifier field: MaxLengthProperty.

Setting a value of 0 indicates that there is no restriction on the number of characters.

Setting MaxLength only restricts the number of characters a user may input. It does not restrict the programmatic input. If the number of characters programmatically added to the text box is greater than MaxLength, then the user may delete characters, but not add any. Once the number of characters is less than MaxLength, the user may add text again.

It is allowed for the user to undo previous actions and cause the text to exceed the MaxLength.

If the user pastes text into the text box that causes the number of characters to exceed MaxLength, then the last characters in the text box are deleted.

Surrogate characters are treated as one character for the purposes of determining text length.

The following code snippets show how you can use the MaxLength property in XAML and code.


<StackPanel>
    <TextBox Margin="20,20,0,0" Text="A text box that demonstrates TextWrapping, TextAlignment, MaxLength, and AcceptsReturn" Width="300" Height="50" TextWrapping="Wrap" TextAlignment="Center" MaxLength="500" AcceptsReturn="True" />
    <TextBox Margin="20,20,0,0" Text="A text box that demonstrates HorizontalScrollBarVisibility and VerticalScrollBarVisibility" Width="300" Height="50" AcceptsReturn="True" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" />
</StackPanel>



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

Private Sub CreateControls()

    'Create stack panel
    Dim MyStackPanel As StackPanel = New StackPanel

    'Create the first text box
    Dim MyTB1 As TextBox = New TextBox
    MyTB1.Width = 300
    MyTB1.Height = 50
    MyTB1.Text = "A text box that demonstrates TextWrapping, TextAlignment, MaxLength and AcceptsReturn"
    MyTB1.TextWrapping = TextWrapping.Wrap
    MyTB1.TextAlignment = TextAlignment.Center
    MyTB1.MaxLength = 500
    MyTB1.AcceptsReturn = True
    MyTB1.Margin = New Thickness(20, 20, 0, 0)

    'Create the second text box
    Dim MyTB2 As TextBox = New TextBox
    MyTB2.Margin = New Thickness(20, 20, 0, 0)
    MyTB2.Text = "A text box that demonstrates HorizontalScrollBarVisibility and VerticalScrollBarVisibility"
    MyTB2.Width = 300
    MyTB2.Height = 50
    MyTB2.AcceptsReturn = True
    MyTB2.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible
    MyTB2.VerticalScrollBarVisibility = ScrollBarVisibility.Visible

    'Add the text boxes to the stack panel
    MyStackPanel.Children.Add(MyTB1)
    MyStackPanel.Children.Add(MyTB2)
    Me.Content = MyStackPanel
End Sub


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.