Hola aquí está un ejemplo práctico. Me parece el código esta bien explicito, el método validated se utiliza para ejecutar la función que mejor te parezca cuando tengas validado el campo:
(Fuente MSDN)
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 '
[email protected]' ";
return false;
}
También podrias hacerlo con un "if-else" en el evento validating y así te olvidarías de usar el validated. Ejemplo:
Private Sub NombreTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles NombreTextBox.Validating
If Me.NombreTextBox.TextLength = 0 Then
Me.ErrorProvider1.SetError(NombreTextBox, "Este campo no puede estar en blanco")
Else
Me.ErrorProvider1.SetError(NombreTextBox, Nothing)
End If
End Sub