views:

1024

answers:

5

Good morning,

I am working on a C# winform application that is using validation for the controls. The issue I'm having is that when a user clicks into a textbox and attempts to click out, the validation fires and re-focuses the control, basically the user cannot click out of the control to another control.

My desired result is to have ALL of the controls on the form validate when the user clicks the submit button. I would like to have the errorProvider icon appear next to the fields that are in error and allow the user to correct them as they see fit.

My question is, how do I setup a control to allow a user to click outside of it when there is an error. I'd like the user to have the ability to fill in the rest of the form and come back to the error on their own instead of being forced to deal with it immediately.

Thank you in advance for any help and advice,

Scott Vercuski [email protected]

+3  A: 

The simplest way would be just to put all the validation in the Submit button handler, instead of having it in the controls.

Jon Skeet
A: 

There's a property (I think it's on the form) that allows you to move between fields when validation fails. I can't remember what it's called, but I think it's named pretty descriptively.

David Kemp
There's the CausesValidation property, which I believe you set on the target control - but you'd need to set it to false for *everything*. Simpler just to avoid the whole mechanism, IMO.
Jon Skeet
Maybe it's that - to be fair, we usually do our own validation on button clicks...
David Kemp
+2  A: 

we have a validation function that returns bool if the form is valid and sets all the error providers on the form:

looks like this:

    private void OnSave()
    {
      if(ValidateData())
      {
        //do save
      }
    }

    public bool ValidateData()
    {
        errorProvider.Clear();
        bool valid = true;
        if (this.defectStatusComboBox.SelectedIndex == -1)
        {
            errorProvider.SetError(defectStatusComboBox, "This is a required feild.");
            valid = false;
        }
        //etc...
        return valid;
     }
Hath
+4  A: 

The form has AutoValidate property, that can be set to allow focus change

nihi_l_ist
A: 

Hi All,

The form property is "AutoValidate" and it affects all controls on the form. It's an enum; set it to System.Windows.Forms.AutoValidate.EnableAllowFocusChange to allow user to exit a control which failed validation, but still show the error icon which allows the user to pull up error tooltip.

The control "CausesValidation" property is a boolean. If it's true then the control raises the validation event which triggers the errorProvider. It it's false, everything gets bypassed, user can exit control, but there's no error icon or tooltip.

Ned Ames