TextBoxBase.MaxLength 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置用户可在文本框控件中键入或粘贴的最大字符数。
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
属性值
可以在文本框控件中输入的字符数。 默认值为 32767。
例外
分配给属性的值小于零。
示例
下面的代码示例使用派生类 TextBox创建用于接受密码的文本框。 此示例使用 CharacterCasing 该属性更改键入为大写的所有字符,并将 MaxLength 密码长度限制为 8 个字符。 此示例还使用 TextAlign 属性在控件中 TextBox 居中设置密码。
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
注解
可以使用此属性来限制在控件中输入的值(如邮政编码和电话号码)中的文本长度,或限制在数据库中输入数据时输入的文本长度。 可以将输入到控件中的文本限制为数据库中相应字段的最大长度。