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

前端 未结 17 1190
一生所求
一生所求 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:51

    In complement of the answer of Daniel Schaffer: if the validation occurs when the edit box loses focus, you can forbid the button to activate to bypass local validation and exit anyway.

    public class UnselectableButton : Button
    {
        public UnselectableButton()
        {
            this.SetStyle(ControlStyles.Selectable, false);
        }
    }
    

    or if you use DevExpress:

    this.simpleButtonCancel.AllowFocus = false;
    

    Note that doing so will change the keyboard experience: the tab will focus anymore on the cancel button.

提交回复
热议问题