Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Propiedad Control.CausesValidation

 

Publicado: octubre de 2016

Obtiene o establece un valor que indica si el control hace que se realice una validación de todos los controles que requieren validación cuando reciben el foco.

Espacio de nombres:   System.Windows.Forms
Ensamblado:  System.Windows.Forms (en System.Windows.Forms.dll)

public bool CausesValidation { get; set; }

Valor de propiedad

Type: System.Boolean

Es true si el control hace que se validen los controles que requieren validación cuando reciben el foco; en caso contrario, es false. De manera predeterminada, es true.

If the P:System.Windows.Forms.Control.CausesValidation property is set to false, the E:System.Windows.Forms.Control.Validating and E:System.Windows.Forms.Control.Validated events are suppressed.

The P:System.Windows.Forms.Control.CausesValidation property value is typically set to false for controls such as a Help button.

The following code example uses the derived classT:System.Windows.Forms.TextBox and validates an e-mail address that the user enters. If the e-mail address is not in the standard format (containing "@" and "."), the validation fails, an T:System.Windows.Forms.ErrorProvider icon is displayed, and the event is canceled. One of the buttons on the form has its P:System.Windows.Forms.Control.CausesValidation property set to false. Clicking or setting focus to this button does not trigger validation. This example requires that a T:System.Windows.Forms.TextBox, an T:System.Windows.Forms.ErrorProvider control, and a T:System.Windows.Forms.Button have been created on a form.

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 e-mail address string is not empty.
   if(emailAddress.Length == 0)
   {
      errorMessage = "e-mail address is required.";
         return false;
   }

   // Confirm that there is an "@" and a "." in the e-mail address, and in the correct order.
   if(emailAddress.IndexOf("@") > -1)
   {
      if(emailAddress.IndexOf(".", emailAddress.IndexOf("@") ) > emailAddress.IndexOf("@") )
      {
         errorMessage = "";
         return true;
      }
   }

   errorMessage = "e-mail address must be valid e-mail address format.\n" +
      "For example 'someone@example.com' ";
      return false;
}

.NET Framework
Disponible desde 1.1
Volver al principio
Mostrar: