当验证规则中断时,停止执行并集中表单

本文关键字:执行 集中 表单 验证 规则 中断 | 更新日期: 2023-09-27 18:25:24

当用户单击"确定"按钮时,我验证它的输入,如:

private void btnOk_Click(object sender, EventArgs e)
{
    myTxtField_Validated(this, e);
    myTxtField2_Validated(this, e);
    CustomObject obj = new CustomObject .....
}

在验证中,我使用错误提供程序通知用户字段中的输入必须符合某些条件。

如果验证中断,我如何静默地中断此部分CustomObject obj = new CustomObject ....的执行。它与现有代码一起正常工作,仅用于验证。

当验证规则中断时,停止执行并集中表单

使用私有布尔成员可以轻松实现这一点,并实现如下:

private bool _validatedTextFields;
private void btnOk_Click(object sender, EventArgs e)
{
   myTxtField_Validated(this, e);
   myTxtField2_Validated(this, e);
   if (_validatedTextFields)
   {
   CustomObject obj = new CustomObject .....
   }
}
private void myTxtField_Validated(object sender, EventArgs e)
{
  //your validation...
  _validatedTextFields = true //only set your member to true if it has been validated.
}
private void myTxtField2_Validated(object sender, EventArgs e)
{
  //your validation...
  _validatedTextFields = true //only set your member to true if it has been validated.
}