使用 winform 的 controlbox[X] 跳过验证? [英] Skip validation with controlbox[X] of winform?
问题描述
我有一个验证事件
private void EmployeeIDtextBox_Validating(object sender, CancelEventArgs e)
{
if (EmployeeIDtextBox.Text == "")
{
MessageBox.Show("Please Enter EmployeeID.", "Invalid EmployeeID");
}
}
并且可以使用取消按钮跳过验证
And able to skip the validation with a cancelbutton
private void cancelbutton_Click(object sender, EventArgs e)
{
AutoValidate = AutoValidate.Disable;
Close();
}
是否可以使用 windowsform 的 controlbox[X] 跳过验证?我试图将表单的 CausesValidation 设置为 false,但它不起作用.我也用 formclosure 尝试过,但它不起作用.
Is it possible to skip validation with controlbox[X] of windowsform? I tried to set CausesValidation of form to false but it is not working. I also try it with formclosing but is it not working.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (EmployeeIDtextBox.CausesValidation)
{
EmployeeIDtextBox.CausesValidation = false;
Close();
}
}
推荐答案
我已经在上一个问题中给了你答案.停止使用 MessageBox,您的问题就会消失.请改用 ErrorProvider 组件.
I already gave you the answer in your previous question. Stop using MessageBox and your problem disappears. Use the ErrorProvider component instead.
拦截表单的关闭处理,以便您可以在第一个事件触发之前取消验证,这需要破解.将此代码粘贴到您的表单中:
Intercepting the form's closing handling so you can cancel the validation before the first event fires requires a hack. Paste this code into your form:
protected override void WndProc(ref Message m) {
// Intercept WM_SYSCOMMAND, SC_CLOSE
if (m.Msg == 0x112 && (m.WParam.ToInt32() & 0xfff0) == 0xf060) this.AutoValidate = AutoValidate.Disable;
base.WndProc(ref m);
}
这篇关于使用 winform 的 controlbox[X] 跳过验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!