我正在尝试使用ErrorProvider组件。
我在表单上有一个关闭按钮,当然它在表单的NE角落也有“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();
}
答案 0 :(得分:3)
您可以尝试在关闭按钮处理程序中清除SetError:
private void buttonCancel_Click(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
formValidation.SetError(ctrl, string.Empty);
}
Close();
}
另外请仔细检查你的buttonCancel是否真的与这个处理程序挂钩。设置一个断点,确保你至少进入这个功能。