【问题标题】:How can I put a muzzle on the ErrorProvider component?我怎样才能在 ErrorProvider 组件上放一个枪口?
【发布时间】:2012-04-12 22:57:51
【问题描述】:

我正在尝试使用 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(); 
        }

【问题讨论】:

标签: c# winforms errorprovider


【解决方案1】:

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

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

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

【讨论】:

  • 这给了我,“不能分配给'SetError',因为它是一个'方法组'”
  • @Clay,你能告诉我你的按钮点击处理程序中的代码吗?当我从内存中发布代码时,我还更正了代码。
  • 张贴在上面。 “就是这个!” (电影引用达到最低字符要求)。
  • @ClayShannon,更新了代码,尽管我不相信 errorProvided 会阻止您的程序关闭。在from没有错误的情况下可以关闭它吗?
  • 不,它没有到达那里。但我认为这是因为我的反应是将焦点设置到出现错误的 TextBox 上。我会评论一下,看看会发生什么......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 2019-09-01
  • 2019-11-07
  • 2016-10-08
  • 2015-03-21
相关资源
最近更新 更多