I have a small problem with validating userinput in textboxes.
There is a usercontrol with various textboxes in which users can enter data to add a new record to the database. When either of these textboxes does not have a correct value (null or specific) I want the inserting to be cancelled and have the 'errormaking' textbox receive focus.
This has to be checked via a button. Can I call the Validating event of the button, or is there a different (better) way to acchieve this?
Also, there is a Cancel button to cancel the entire inserting. When trying to use the errorprovider, the Cancel button became unavailable aswell, because the field were empty so it could not send.
Some code for reference:
private void CheckInput(CancelEventArgs e, TextBox tb)
{
ErrorProvider error = new ErrorProvider();
if (string.IsNullOrEmpty(tb.Text))
{
error.SetError(tb, "*");
e.Cancel = true;
}
if (!string.IsNullOrEmpty(tb.Text))
{
error.SetError(tb, string.Empty);
error.Clear();
error.Dispose();
}
}