Control.CausesValidation Proprietà

Definizione

Ottiene o imposta un valore che indica se viene eseguita la convalida di tutti i controlli per cui è richiesta quando il controllo riceve lo stato attivo.

C#
public bool CausesValidation { get; set; }

Valore della proprietà

true se viene eseguita la convalida di tutti i controlli per cui è richiesta quando il controllo riceve lo stato attivo; in caso contrario, false. Il valore predefinito è true.

Esempio

Nell'esempio di codice seguente viene usata la classe TextBox derivata e viene convalidato un indirizzo di posta elettronica immesso dall'utente. Se l'indirizzo di posta elettronica non è nel formato standard (contenente "@" e "."), la convalida ha esito negativo, viene visualizzata un'icona ErrorProvider e l'evento viene annullato. Uno dei pulsanti del modulo ha la relativa CausesValidation proprietà impostata su false. Fare clic o impostare lo stato attivo su questo pulsante non attiva la convalida. In questo esempio è necessario che un TextBoxErrorProvider controllo e un Button controllo siano stati creati in un modulo.

C#
public Form1()
{
    InitializeComponent();
    //Set button2 to be non-validating.
    this.button2.CausesValidation = false;
}

private void textBox1_Validating(object sender, 
                System.ComponentModel.CancelEventArgs e)
{
   string errorMsg;
   if(!ValidEmailAddress(textBox1.Text, out errorMsg))
   {
      // Cancel the event and select the text to be corrected by the user.
      e.Cancel = true;
      textBox1.Select(0, textBox1.Text.Length);

      // Set the ErrorProvider error with the text to display. 
      this.errorProvider1.SetError(textBox1, errorMsg);
   }
}

private void textBox1_Validated(object sender, System.EventArgs e)
{
   // If all conditions have been met, clear the ErrorProvider of errors.
   errorProvider1.SetError(textBox1, "");
}
public bool ValidEmailAddress(string emailAddress, out string errorMessage)
{
   // Confirm that the email address string is not empty.
   if(emailAddress.Length == 0)
   {
      errorMessage = "email address is required.";
         return false;
   }

   // Confirm that there is an "@" and a "." in the email address, and in the correct order.
   if(emailAddress.IndexOf("@") > -1)
   {
      if(emailAddress.IndexOf(".", emailAddress.IndexOf("@") ) > emailAddress.IndexOf("@") )
      {
         errorMessage = "";
         return true;
      }
   }
   
   errorMessage = "email address must be valid email address format.\n" +
      "For example 'someone@example.com' ";
      return false;
}

Commenti

Se la CausesValidation proprietà è impostata su false, gli Validating eventi e Validated vengono eliminati.

Il valore della CausesValidation proprietà è in genere impostato su false per i controlli, ad esempio un pulsante della Guida.

Si applica a

Prodotto Versioni
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Vedi anche