I'm doing validations in C # with ErrorProvider.
Validations are about TextBox
.
//Llama al método valida para decidir si el TextBox está vacío.
private void textBox1_Validating(object sender, CancelEventArgs e)
{
valida(sender, e, textBox1, errorProvider1);
}
//Método que recibe un textbox y un Error provider para validar si el textBox esta vacio.
private void valida(object sender, CancelEventArgs e, TextBox tb, ErrorProvider error)
{
if (string.IsNullOrEmpty(tb.Text))
{
e.Cancel = true;
error.SetError(tb, "Campos Vacios");
}
}
//valida cuando el TextBox pierde el foco y quita el ErrorProvider si no está vacío
private void textBox1_Validated(object sender, EventArgs e)
{
errorProvider1.SetError(textBox1, "");
}
//Cerrar ventana
private void btnCerrar(object sender, EventArgs e)
{
this.Close();
}
The problem I have when the ErrorProvider
is activated so to speak. If the TextBox
that I'm validating this empty shows the ErrorProvider
, and the cursor stays in that TextBox
empty waiting to be validated again.
But:
Why can not I close the window when
ErrorProvider
is active?Why can not I give Tab or change the cursor while
ErrorProvider
is active?
It's a desktop application made in C #.
When the form has that error sign, I can not change the cursor, let alone close the window.
EDIT:
If I remove the e.cancel = true;
it does not show me the ErrorProvider
EDIT2
In the event Form1_FormClosing
of the form I did what @JLPrieto e.cancel = false;
said, which works only when the X of the form is pressed and the form closes. But if I do it from the button1 this.close()
it does not close the window when I press the button.