none
ErrorProvider and Validating Event -- Cancel Button doesn't work RRS feed

  • Question

  • Hello,

     

    I have a user control form with some textboxes, a Save button, and a Cancel button.  I'm using an ErrorProvider for validating the textboxes.  I'm using the Validating events of the textboxes to SetError on the error provider if the text is invalid.  My problem is that I wan't the Cancel button to close the form whenever it is clicked, however, when validation fires it is preventing the Cancel button from working

     

    I've searched many forums and the majority of them say the solution is to set the CausesValidation property to "false" on the Cancel Button.  This doesn't work for me.  The Validating Event is called whenever you try to focus out of the textbox so it always gets called before the click event of the Cancel button.  In the Validating event, if the text in the textbox

    is invalid, I need to set the CancelEventArg Cancel property to true.  This prevents it from getting to the click event of the Cancel button.

     

    Is there a way to get this to work?  Or do I have the wrong approach?  Should I not be using the Validate Events at all?

     

    thanks so much

    -justme23

     

     

     

     

     

    Tuesday, September 23, 2008 6:07 PM

Answers

  • Erm, that doesn't make sense.  With the Cancel button's CausesValidation set to False, you'll have no trouble setting the focus on the button and clicking it.  Sample code:

      public partial class Form1 : Form {
        public Form1() {
          InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e) {
          new Form2().ShowDialog(this);
        }
      }

      public partial class Form2 : Form {
        public Form2() {
          InitializeComponent();
          btnCancel.CausesValidation = false;
        }
        private void textBox1_Validating(object sender, CancelEventArgs e) {
          e.Cancel = true;
        }
        private void btnCancel_Click(object sender, EventArgs e) {
          this.DialogResult = DialogResult.Cancel;
        }
      }

    No trouble closing the dialog.

    Wednesday, September 24, 2008 6:29 PM
    Moderator

All replies

  • This will happen when you don't use ShowDialog().  You probably should, a workaround is to add a FormClosing event handler:

            private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
                e.Cancel = false;
            }

    Tuesday, September 23, 2008 8:07 PM
    Moderator
  • Thanks for your reply.  I actualy am using ShowDialog() to bring up the form.   How do i use the Form Closing Event Handler?  Is that event fired when you close with the standard close button ( 'X' button)?  I'm able to close the form with that button, but I also have my own Cancel button which I want to behave the same way. 

     

    Thanks

    Tuesday, September 23, 2008 8:58 PM
  • Your Cancel button should set this.DialogResult = DialogResult.Cancel, not call this.Close().  The dialog closing is automatic after that and it will bypass the validation.
    Tuesday, September 23, 2008 9:40 PM
    Moderator
  •  

    But it cant reach the click event handler of the Cancel button.  The Validating event handler on the textbox gets called first, and in that handler, when no text has been set in the textbox, I set the Cancel property of the CancelEventArgs argument to true, cutting the event chain so it never reaches the Cancel button click event.  I believe I need to set Cancel to true there to make the validation work (otherwise someone can leave the the textbox blank, leave the textbox and cllick the Save button -- bad. ) 

     

    thanks again.

    Wednesday, September 24, 2008 2:45 PM
  • Erm, that doesn't make sense.  With the Cancel button's CausesValidation set to False, you'll have no trouble setting the focus on the button and clicking it.  Sample code:

      public partial class Form1 : Form {
        public Form1() {
          InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e) {
          new Form2().ShowDialog(this);
        }
      }

      public partial class Form2 : Form {
        public Form2() {
          InitializeComponent();
          btnCancel.CausesValidation = false;
        }
        private void textBox1_Validating(object sender, CancelEventArgs e) {
          e.Cancel = true;
        }
        private void btnCancel_Click(object sender, EventArgs e) {
          this.DialogResult = DialogResult.Cancel;
        }
      }

    No trouble closing the dialog.

    Wednesday, September 24, 2008 6:29 PM
    Moderator
  • Your right.  I found the problem.  I had the Cancel button's CausesValidation = False, but my Cancel button is contaied in a Panel, which is inside of a TableLayoutPanel, which is inside of GroupBox, etc.  All of these containers had CausesValidation = True.  I set them all to False and it works now.  Somehow it hit me to check these after I saw your post.

     

    thanks for your help!

    Wednesday, September 24, 2008 7:37 PM