1

我正在尝试使用 ErrorProvider 组件。

我在表格上有一个关闭按钮,当然它在表格的东北角也有“X”关闭的东西。

但是,一旦“引发”错误,单击“关闭”按钮或“关闭”框(或称为 doohicky 的任何内容)将无响应/不起作用。

当表单上有错误时,我必须怎么做才能让用户关闭表单?

更新:

这是我现在在“关闭”按钮的 OnClick() 处理程序中尝试的代码 - 它仍然拒绝关闭:

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

再次更新:只是为了做鬼脸,我尝试在“关闭”按钮上将“DialogResult”属性从 Canceled 更改为 None,但这并没有帮助(没想到它会 - 抓住稻草)

也没有将按钮的“原因验证”属性从 True 更改为 False ......

再次更新:

以下是所有可能适合发布或不适合发布的相关内容:

        . . .
              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(); 
        }
4

1 回答 1

3

您可以尝试在关闭按钮处理程序中清除 SetError:

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

还要仔细检查您的 buttonCancel 是否确实连接到此处理程序。放置一个断点并确保您至少进入了这个函数。

于 2012-04-12T23:05:23.593 回答