How to skip Validating after clicking on a Form's Cancel button

前端 未结 17 1229
一生所求
一生所求 2020-12-08 04:04

I use C#. I have a Windows Form with an edit box and a Cancel button. The edit box has code in validating event. The code is executed every time the edit box loses focus. Wh

17条回答
  •  半阙折子戏
    2020-12-08 04:32

    None of these answers quite did the job, but the last answer from this thread does. Basically, you need to:

    1. Insure that the Cancel button (if any) has .CausesValidation set to false
    2. Override this virtual method.

      protected override bool ProcessDialogKey(Keys keyData) {
          if (keyData == Keys.Escape) {
              this.AutoValidate = AutoValidate.Disable;
              CancelButton.PerformClick();
              this.AutoValidate = AutoValidate.Inherit;
              return true;
          }
          return base.ProcessDialogKey(keyData);
      }
      

    I didn't really answer this, just pointing to the two guys who actually did.

提交回复
热议问题