Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TextBox.MaxLength Property

Gets or sets a value indicating the maximum number of characters that can be manually entered into the TextBox.

Namespace: System.Windows.Controls
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:

[LocalizabilityAttribute(LocalizationCategory.None, Modifiability=Modifiability.Unmodifiable)] 
public int MaxLength { get; set; }
/** @property */
public int get_MaxLength ()

/** @property */
public void set_MaxLength (int value)

public function get MaxLength () : int

public function set MaxLength (value : int)

<object MaxLength="int" .../>

Property Value

An integer indicating the maximum number of characters that can be manually entered into the TextBox. The default value is zero (0).

Identifier Field


Metadata Flags


You can use this property to restrict the length of text entered in the control for values such as postal codes and telephone numbers. You can also use this property to restrict the length of text entered when the data is to be stored in a database so that the text entered into the control doesn't exceed the maximum length of the corresponding field in the database.

This property does not affect characters that are added programmatically.

When this property is set to zero, the maximum length of the text that can be entered in the control is limited only by available memory.

The following example shows how to create a TextBox with a MaxLength of 500 characters.

<Page  xmlns=""
      Text="Initial text in TextBox" 
      MaxLines="5" />

StackPanel myStackPanel = new StackPanel();

//Create TextBox
TextBox myTextBox = new TextBox();
myTextBox.Width = 200;

// Put some initial text in the TextBox.
myTextBox.Text = "Initial text in TextBox";

// Set the maximum characters a user can manually type
// into the TextBox.
myTextBox.MaxLength = 500;
myTextBox.MinLines = 1;

// Set the maximum number of lines the TextBox will expand to 
// accomidate text. Note: This does not constrain the amount of 
// text that can be typed. To do that, use the MaxLength property.
myTextBox.MaxLines = 5;

// The text typed into the box is aligned in the center.
myTextBox.TextAlignment = TextAlignment.Center;

// When the text reaches the edge of the box, go to the next line.
myTextBox.TextWrapping = TextWrapping.Wrap;

this.Content = myStackPanel;

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 Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

© 2017 Microsoft