我已经在我的取消按钮上将causevalidation设置为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();
}
或
private void button3_Click(object sender, EventArgs e)
{
AutoValidate = AutoValidate.Disable;
Close();
}
我需要在Windowform的Close [X]框中将CauseValidation设置为false。 我已经尝试在表单本身中将CauseValidation设置为false但它不起作用。每当我点击关闭[X]框时,消息框仍然会出现。
答案 0 :(得分:3)
Form类在关闭表单之前自动运行ValidateChildren。如果您有任何控件在其Validating事件处理程序中设置e.Cancel = true,则会阻止“关闭”按钮起作用。您所要做的就是让表单无论如何都要关闭。将此代码粘贴到表单的源代码中:
protected override void OnFormClosing(FormClosingEventArgs e) {
e.Cancel = false;
base.OnFormClosing(e);
}
如果你抱怨MessageBox.Show()而不是ErrorProvider 和你用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);
}
答案 1 :(得分:0)
if (this.IsDisposed)
{
if (EmployeeIDtextBox.Text == "")
{
MessageBox.Show("Please Enter EmployeeID.", "Invalid EmployeeID");
}
}