Export (0) Print
Expand All

TextBoxBase.MaxLength Property

Gets or sets the maximum number of characters the user can type or paste into the text box control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

[LocalizableAttribute(true)] 
public virtual 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)

Property Value

The number of characters that can be entered into the control. The default is 32767.

Exception typeCondition

ArgumentOutOfRangeException

The value assigned to the property is less than 0.

You can use this property to restrict the length of text entered in the control for values such as postal codes and telephone numbers, or to restrict the length of text entered when the data is to be entered in a database. You can limit the text entered into the control to the maximum length of the corresponding field in the database.

NoteNote

In code, you can set the value of the Text property to a value that has a length greater than the value specified by the MaxLength property. This property only affects text entered into the control at run time.

Windows NT 4.0, Windows 2000, Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003 Platform Note: For single line text box controls, if the MaxLength property is set to 0, the maximum number of characters the user can enter is 2147483646 or an amount based on available memory, whichever is smaller. For multiline text box controls, the maximum number of characters the user can enter is 4294967295 or an amount based on available memory, whichever is smaller.

Windows Millennium Edition Platform Note: For single line text box controls, if the MaxLength property is set to 0, the maximum number of characters the user can enter is 32,766 or an amount based on available memory, whichever is smaller. For multiline text box controls, the maximum number of characters the user can enter is 65,535 or an amount based on available memory, whichever is smaller.

The following code example uses the derived class, TextBox, to create a text box that is used to accept a password. This example uses the CharacterCasing property to change all characters typed to uppercase and the MaxLength property to restrict the password length to eight characters. This example also uses the TextAlign property to center the password in the TextBox control.

public void CreateMyPasswordTextBox()
 {
    // Create an instance of the TextBox control.
    TextBox textBox1 = new TextBox();
    // Set the maximum length of text in the control to eight.
    textBox1.MaxLength = 8;
    // Assign the asterisk to be the password character.
    textBox1.PasswordChar = '*';
    // Change all text entered to be uppercase.
    textBox1.CharacterCasing = CharacterCasing.Upper;
    // Align the text in the center of the TextBox control.
    textBox1.TextAlign = HorizontalAlignment.Center;
 }

public void CreateMyPasswordTextBox()
{
    // Create an instance of the TextBox control.
    TextBox textBox1 = new TextBox();
    // Set the maximum length of text in the control to eight.
    textBox1.set_MaxLength(8);
    // Assign the asterisk to be the password character.
    textBox1.set_PasswordChar('*');
    // Change all text entered to be uppercase.
    textBox1.set_CharacterCasing(CharacterCasing.Upper);
    // Align the text in the center of the TextBox control.
    textBox1.set_TextAlign(HorizontalAlignment.Center);
} //CreateMyPasswordTextBox

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft