This topic has not yet been rated - Rate this topic

TextBoxBase.MaxLength Property

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

[Visual Basic]
Public Overridable Property MaxLength As Integer
[C#]
public virtual int MaxLength {get; set;}
[C++]
public: __property virtual int get_MaxLength();
public: __property virtual void set_MaxLength(int);
[JScript]
public function get MaxLength() : int;
public function set MaxLength(int);

Property Value

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

Exceptions

Exception Type Condition
ArgumentException The value assigned to the property is less than 0.

Remarks

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.

Note   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 Server 2003 family 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 95, Windows 98, Windows 98 Second Edition, 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.

Example

[Visual Basic, C#, C++] The following 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.

[Visual Basic] 
Public Sub CreateMyPasswordTextBox()
    ' Create an instance of the TextBox control.
    Dim textBox1 As 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 = "*"c
    ' Change all text entered to be lowercase.
    textBox1.CharacterCasing = CharacterCasing.Upper
    ' Align the text in the center of the TextBox control.
    textBox1.TextAlign = HorizontalAlignment.Center
End Sub

[C#] 
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 lowercase.
    textBox1.CharacterCasing = CharacterCasing.Upper;
    // Align the text in the center of the TextBox control.
    textBox1.TextAlign = HorizontalAlignment.Center;
 }

[C++] 
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 lowercase.
    textBox1->CharacterCasing = CharacterCasing::Upper;
    // Align the text in the center of the TextBox control.
    textBox1->TextAlign = HorizontalAlignment::Center;
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

TextBoxBase Class | TextBoxBase Members | System.Windows.Forms Namespace

Did you find this helpful?
(1500 characters remaining)