user26901
user26901

Reputation:

Textbox validation, focus switching issue

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,

Upvotes: 6

Views: 8864

Answers (5)

Ned Ames
Ned Ames

Reputation: 71

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.

Upvotes: 7

0x49D1
0x49D1

Reputation: 8704

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

Upvotes: 29

Hath
Hath

Reputation: 12769

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;
     }

Upvotes: 2

kͩeͣmͮpͥ ͩ
kͩeͣmͮpͥ ͩ

Reputation: 7846

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.

Upvotes: -1

Jon Skeet
Jon Skeet

Reputation: 1500065

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

Upvotes: 2

Related Questions