TextBoxBase.MaxLength 属性

定义

获取或设置用户可在文本框控件中键入或粘贴的最大字符数。Gets or sets the maximum number of characters the user can type or paste into the text box control.

public:
 virtual property int MaxLength { int get(); void set(int value); };
public virtual int MaxLength { get; set; }
member this.MaxLength : int with get, set
Public Overridable Property MaxLength As Integer

属性值

可以在文本框控件中输入的字符数。The number of characters that can be entered into the control. 默认值为32767。The default is 32767.

异常

分配给属性的值小于零。The value assigned to the property is less than 0.

示例

下面的代码示例使用派生类 TextBox来创建用于接受密码的文本框。The following code example uses the derived class, TextBox, to create a text box that is used to accept a password. 此示例使用 CharacterCasing 属性将键入的所有字符更改为大写,并使用 MaxLength 属性将密码长度限制为8个字符。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. 此示例还使用 TextAlign 属性使密码在 TextBox 控件中居中。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 = gcnew 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.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 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

注解

您可以使用此属性来限制控件中为邮政编码和电话号码等值输入的文本长度,或限制在数据库中输入数据时输入的文本长度。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.

备注

在代码中,你可以将 Text 属性的值设置为长度大于 MaxLength 属性指定的值的值。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.

适用于