Gets or sets the
maximum number of characters the user can type or paste into the text box
control. Every textbox has a
MaxLength property
you could test for. Text boxes have a .Length API call which will tell you how many
characters are currently in the text box. It looks something like this:
C# Syntax
If(textBox1.Text.Length >= 50)
{
MessageBox.Show("Invalid
input. Value must be less than 50 characters");
}
Else
{
//Acceptable input length
}
Example
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 http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.charactercasing.aspx 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 http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.textalign.aspx property
to center the password in the http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.aspx control.
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 uppercase.
textBox1.CharacterCasing =
CharacterCasing.Upper
' Align the text in the center of the
TextBox control.
textBox1.TextAlign =
HorizontalAlignment.Center
End Sub