TextBoxBase.MaxLength 속성

정의

TextBox 컨트롤에 입력하거나 붙여넣을 수 있는 최대 문자 수를 가져오거나 설정합니다.

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

속성 값

Int32

컨트롤에 입력할 수 있는 문자 수입니다. 기본값은 32767입니다.

예외

속성에 할당된 값이 0보다 작은 경우

예제

다음 코드 예제에서는 파생 클래스 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

설명

이 속성을 사용하여 우편 번호 및 전화 번호와 같은 값에 대해 컨트롤에 입력한 텍스트의 길이를 제한하거나 데이터베이스에 데이터를 입력할 때 입력한 텍스트의 길이를 제한할 수 있습니다. 컨트롤에 입력한 텍스트를 데이터베이스에 있는 해당 필드의 최대 길이로 제한할 수 있습니다.

참고

코드에서는 속성 값 Text 이 속성에 지정된 값보다 긴 값으로 MaxLength 설정할 수 있습니다. 이 속성은 런타임에 컨트롤에 입력한 텍스트에만 영향을 줍니다.

적용 대상