我已经将取消按钮上的 CausesValidation 设置为 false 并且它可以工作。
bool IsCancelBtnClicked = false;
private void EmployeeIDtextBox_Validating(object sender, CancelEventArgs e)
{
if (EmployeeIDtextBox.Text == "")
{
MessageBox.Show("Please Enter EmployeeID.", "Invalid EmployeeID");
}
}
private void button3_Click(object sender, EventArgs e)
{
IsCancelBtnClicked = true;
EmployeeIDtextBox.Validating -= new CancelEventHandler(textBox4_Validating);
this.Close();
}
or
private void button3_Click(object sender, EventArgs e)
{
AutoValidate = AutoValidate.Disable;
Close();
}
我需要的是在 windowsform? 的 Close[X] 框中将 CauseValidation 设置为 false?。
我已经尝试在表单本身中将 CauseValidation 设置为 false,但它不起作用。每次我点击关闭[X]框时,消息框仍然会出现。
Form 类在关闭表单之前自动运行 ValidateChildren。如果您有任何控件在其验证事件处理程序中设置了 e.Cancel = true,则这会阻止“关闭”按钮工作。您所要做的就是让表单关闭。将此代码粘贴到表单的源代码中:
protected override void OnFormClosing(FormClosingEventArgs e) {
e.Cancel = false;
base.OnFormClosing(e);
}
如果您抱怨 MessageBox.Show() 而不是 ErrorProviderand如果您使用 Show() 而不是 ShowDialog() 显示窗口,那么您需要更大的武器。这需要在 Winforms 运行 ValidateChildren() 方法并触发消息框之前尽早禁用验证。将此代码粘贴到表单类中:
protected override void WndProc(ref Message m) {
const int WM_CLOSE = 0x10;
if (m.Msg == WM_CLOSE) {
base.AutoValidate = System.Windows.Forms.AutoValidate.Disable;
}
base.WndProc(ref m);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)