1

Im working with errorprovider in a c# winforms application. Now I want to have a "double" validation. Once on the textfields directly, so the user sees that he has made some errors, and once on the button itself. So when there are still errors, the "save" button will keep greyed out or "disabled".

Because I don't want to block my user when he is making an error, and I want him to be able to make the changes whenever he wants im using the event "leave" or on focus lost. This because otherwise I noticed you cannot go to another field, until you changed your error.

So, now the code:

    private void txtFirstname_Leave(object sender, EventArgs e)
    {
        if (!InputChecks.IsFilledIn(txtFirstname.Text))
        {
            errorProvider1.SetError(txtFirstname, "Firstname needs to be filled in!");
            isValidated = false;

        }
        else
        {
            errorProvider1.SetError(txtFirstname, "");
            isValidated = true;

        }
    }

So far, so good. The error provider works correctly and my user can edit whenever he wants.

 public void setSaveButton()
    {
        if (isValidated == true)
        {
            btnSave.Enabled = true;

        }
        else
        {
            btnSave.Enabled = false;
        }   
    }

 bool isValidated;
    private void btnSave_Click(object sender, EventArgs e)
    {

        if (isValidated == true)
        {

            employeePresenter.addEmployee(txtFirstname.Text, txtLastname.Text, txtUsername.Text, txtPassword.Text);
        }



    }

This was still okey in my head. BUT, as I give the ability to the user to change the issues whenever they want, this doesn't work. I tried to put the method "setSaveButton()" under "isvalidated" but this is not working either. Because of the focus lost.

Anyone has a better idea for this? I have been looking on google and the only things i found was a single validation with the errorprovider, or the event validating. But these events don't allow users to edit their errors whenever they want. It blocks them into one particular text field.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Kevin
  • 151
  • 4
  • 15
  • And how does this ValidateChildren() exaclty works? Because the example on the microsoft website is not really clear. Its actually not working, so i must do something wrong... https://msdn.microsoft.com/en-us/library/ms158374(v=vs.110).aspx – Kevin Apr 24 '16 at 16:46
  • 1
    Having to guess at "it does not work", you probably never set e.Cancel = true in your Validating event handler. So the ValidateChildren() method does not know that anything did not validate. Another way to do it is to count the number or error icons you display, consider using [this class](http://stackoverflow.com/a/2682478/17034). – Hans Passant Apr 24 '16 at 16:57
  • Hans, the "it does not work" was a bit bluntly. I infact set the e.cancel, but then again i get the scenario of locking my user into one textfield. Wich is not so user-friendly in my opnion. The problem i faced was i never went into the else operator. In other words, i never had the "validation failed". – Kevin Apr 24 '16 at 17:41
  • @Kevin To let the user to move between controls even if there is an error, set `AutoValidate` property of your `Form` to `EnableAllowFocusChange`. – Reza Aghaei Apr 24 '16 at 20:34

1 Answers1

3

You don't need to make the save button disabled. It's enough to check ValidateChildren method of your form and if it returned false, it means there is some validation error. To use this approach you should remember to set e.Cancel = true in Validating event of the control when you set an error for control.

Also to let the user to move between controls even if there is an error, set AutoValidate property of your Form to EnableAllowFocusChange in designer or using code:

this.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange;

Code for Validation:

private void txtFirstname_Validating(object sender, CancelEventArgs e)
{
    if (string.IsNullOrEmpty(this.txtFirstname.Text))
    {
        this.errorProvider1.SetError(this.txtFirstname, "Some Error");
        e.Cancel = true;
    }
    else
    {
        this.errorProvider1.SetError(this.txtFirstname, null);
    }
}

private void btnSave_Click(object sender, EventArgs e)
{
    if (this.ValidateChildren())
    {
        //Here the form is in a valid state
        //Do what you need when the form is valid
    }
    else
    {
        //Show error summary
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398