Question

I'm trying to use an ErrorProvider component.

I have a Close button on the form, and of course it also has the "X" close thingy in the NE corner of the form.

Once an error has been "raised" though, clicking the Close button or the Close box (or whatever that doohicky is called) are unresponsive/nonfunctional.

What must I do to allow the user to dismiss the form when it has errors on it?

Update:

Here's the code I'm trying now in the "Close" button's OnClick() handler - it still refuses to close:

private void buttonCancel_Click(object sender, EventArgs e) {
  formValidation.SetError(this, String.Empty);
  Close();
}

Updated again: Just for grimaces, I tried changing the "DialogResult" property from Canceled to None on the "Close" button, but that did not help (didn't expect it to - grasping at straws)

Neither did changing the button's "Causes Validation" property from True to False...

Updated yet again:

Here's all the pertinent that may or may not be fit to post:

        . . .
              const int MINIMUM_PASSWORD_LENGTH = 5;

        private string originalPassword {
            get { return textCurrentPassword.Text; }
        }

        private string newCandidatePassword1 {
            get { return textNewPassword.Text; }
        }

        private string newCandidatePassword2 {
            get { return textNewPasswordRepeated.Text; }
        }

        public ChangePassword() {
            InitializeComponent();
        }

        private void textCurrentPassword_Validating(object sender, CancelEventArgs e) {
            string error = null;

            if (originalPassword.Equals(String.Empty)) {
                error = currentPasswordInvalid;
                e.Cancel = true;
                //textCurrentPassword.Focus(); probably unnecessary because of .SetError() below
            };

            // TODO: Replace 1==2 with call that compares password with the current user's confirmed password
            if (1 == 2) {
                error = currentPasswordDoesNotMatchCurrentUser;
                e.Cancel = true;
            }

            formValidation.SetError((Control)sender, error);
            if (null != error) {
                ;
            }
        }

        private void textNewPassword_Validating(object sender, CancelEventArgs e) {
            string error = null;

            if (newCandidatePassword1.Length < 5) {
                error = newPasswordInvalid;
                e.Cancel = true;
            }

            formValidation.SetError((Control)sender, error);
            if (null != error) {
                ;
            }
        }

        private void textNewPasswordRepeated_Validating(object sender, CancelEventArgs e) {
            string error = null;

            // Long enough?
            if (newCandidatePassword2.Length < MINIMUM_PASSWORD_LENGTH) {
                error = newPasswordInvalid;
                e.Cancel = true;
            }

            // New passwords match?
            if (!newCandidatePassword2.Equals(newCandidatePassword1)) {
                error = newPasswordsDoNotMatch;
                e.Cancel = true;
            }

            // They match, but all three match (undesirable)
            if (!originalPassword.Equals(newCandidatePassword1)) {
                error = newPasswordSameAsOld;
                e.Cancel = true;
            }

            // Unique across the user base?
            // TODO: Replace 1==2 with call that verifies this password is unique 
            if (1 == 2) {
                error = newPasswordNotUnique;
                e.Cancel = true;
            }

            formValidation.SetError((Control)sender, error);
            if (null != error) {
                ;
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e) {
            foreach (Control ctrl in this.Controls) { 
                formValidation.SetError(ctrl, string.Empty); 
            } 
            Close(); 
        }
Was it helpful?

Solution

You can trying clearing SetError in your Close button handler:

private void buttonCancel_Click(object sender, EventArgs e)
{
    foreach (Control ctrl in this.Controls)
    {
        formValidation.SetError(ctrl, string.Empty);
    }
    Close();
}

Also double check that your buttonCancel is indeed hooked up to this handler. Put a breakpoint and make sure that you are at least getting into this function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top