Condividi tramite


TextBoxBase.MaxLength Proprietà

Definizione

Ottiene o imposta il numero massimo di caratteri che l'utente può digitare o incollare nel controllo casella di testo.

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

Valore della proprietà

Numero di caratteri che possono essere immessi nel controllo . Il valore predefinito è 32767.

Eccezioni

Il valore assegnato alla proprietà è minore di 0.

Esempio

Nell'esempio di codice seguente viene usata la classe derivata , TextBox, per creare una casella di testo usata per accettare una password. In questo esempio viene utilizzata la CharacterCasing proprietà per modificare tutti i caratteri digitati in maiuscolo e la MaxLength proprietà per limitare la lunghezza della password a otto caratteri. In questo esempio viene usata anche la TextAlign proprietà per centrare la password nel TextBox controllo .

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

Commenti

È possibile utilizzare questa proprietà per limitare la lunghezza del testo immesso nel controllo per valori quali codici postali e numeri di telefono oppure per limitare la lunghezza del testo immesso quando i dati devono essere immessi in un database. È possibile limitare il testo immesso nel controllo alla lunghezza massima del campo corrispondente nel database.

Annotazioni

Nel codice è possibile impostare il valore della Text proprietà su un valore con lunghezza maggiore del valore specificato dalla MaxLength proprietà . Questa proprietà influisce solo sul testo immesso nel controllo in fase di esecuzione.

Si applica a