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

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

    I was having problems getting my form to close, since the validation of certain controls was stopping it. I had set the control.CausesValidation = false for the cancel button and all the parents of the cancel button. But still was having problems.

    It seemed that if the user was in the middle of editing a field that was using validation and just decided to give up (leaving the field with an invalid input), the cancel button event was being fired but the window would not close down.

    This was fixed by the following in the cancel button click event:

    private void btnCancel_Click(object sender, EventArgs e)
    {
        // Stop the validation of any controls so the form can close.
        AutoValidate = AutoValidate.Disable;
        Close();
    }
    
    0 讨论(0)
  • 2020-12-08 04:36

    Set the CausesValidation property to false.

    0 讨论(0)
  • 2020-12-08 04:39

    This is an old question however I recently ran into this issue and solved it this way:

    1st, we are loading a UserControl into a 'shell' Form that has a save and cancel button. The UserControl inherit an interface (like IEditView) that has functions for Save, Cancel, Validate and ToggleValidate.

    In the shell form we used the mouse enter and mouse leave like so:

        private void utbCancel_MouseEnter(object sender, EventArgs e)
        {
            ((Interface.IEdit)tlpMain.Controls[1]).ToggleValidate();
        }
    
        private void utbCancel_MouseLeave(object sender, EventArgs e)
        {
            ((Interface.IEdit)tlpMain.Controls[1]).ToggleValidate();
        }
    

    Then in ToggleValidate (Say a simple form with two controls...you can always just loop through a list if you want) we set the CausesValidation

        public bool ToggleValidate()
        {
            uneCalcValue.CausesValidation = !uneCalcValue.CausesValidation;
            txtDescription.CausesValidation = !txtDescription.CausesValidation;
    
            return txtDescription.CausesValidation;
        }
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-08 04:41

    Judicious use of the Control.CausesValidation property will help you achieve what you want.

    0 讨论(0)
  • 2020-12-08 04:43

    By using Visual Studio wizard you can do it like that:

    0 讨论(0)
  • 2020-12-08 04:48

    Setting CausesValidation to false is the key, however this alone is not enough. If the buttons parent has CausesValidation set to true, the validating event will still get called. In one of my cases I had a cancel button on a panel on a form, so I had to set CausesValidation = false on the panel as well as the form. In the end I did this programatically as it was simpler than going through all the forms...

    Control control = cancelButton;
    
    while(control != null)
    {
       control.CausesValidation = false;
       control = control.Parent;
    }
    
    0 讨论(0)
提交回复
热议问题