textbox validating

I ahev a form in vb.net in which the textboxes are validated useing e.cancel.

My problem is I want to have a "clear Form" & an"exit" button. If a textbox has focus and I click a button to clear/exit the button click is not called as the textbox retains focus due to e.canel=true.

How can I get these buttons to work

Colin




Answer this question

textbox validating

  • Rama Satya Jagan K

    Yes you can set the CausesValidation Property of the button to to false. so it will not activate the Validating event of the text box.

    Actully validating event of a control is called when the control lost focus and the control received the focus have the CausesValidation property is true.



  • Faisal Shahzad

    Hi,

    try setting form's AutoValidate property to EnableAllowFocusChange.

    Andrej



  • Nate_NC

    > Anyone else having this problem, or (hopefully) found a way around it

    I added the textbox1.Focus() in the validation as suggested and changed the form's AutoValidate back to EnableAllowFocusChange and that allows my Cancel button to exit without triggering the validation message. However, if I click the X button in the upper-right corner of the window, I still get the validation message prior to the window closing, and if I use the Escape button as you described, I actually get the validation message twice prior to the window closing.


  • intrepid

    The issue you're having with the Save button retaining focus after the validation error message is acknowledged can be handled in the Validating event by setting focus back to the textbox.

    MessageBox.Show(...)

    textbox1.Focus()

    Everything seems to be working for me with the exception of when the user hits the ESC key. I've set the CancelButton property on the form to use the Cancel button (duh!). If the user actually clicks the Cancel button, the form closes and everything is fine. But if they hit ESC, the Validating event fires, and gives them the error message. Then when they acknowledge that, the form closes. This happens no matter which way I set the AutoValidating property on the form. Looks like a bit of a glitch in the internal event handling, to me. Anyone else having this problem, or (hopefully) found a way around it


  • Dark Helmet

    I didn't have those problems, but I ended up using the Leave event instead, because of the problem I was having with the CancelButton trigger. In the Leave routine, I check first thing to see if the Cancel buttun has focus. If it does, then they actually clicked the Cancel button, so I just Exit Sub immediately and skip the validation code. Everything else works just fine.

    I'll be using the Validating event sparingly until they get the kinks ironed out.


  • One from

    Set the ClearButton's CausesValidation = False
  • Hassan Ayoub

    I am having the same problem as the original poster. I have a form with (among other things) a textbox with a textbox1_validating event, a save button, and a cancel button. I have set the cancel button's CausesValidation to false, and have a save button with a CausesValidation of true. I have entered invalid data in the textbox which triggers e.Cancel=True. I wish the validation to be skipped when the cancel button is pressed, but to get called when the save button is pressed.

    If I set the form's AutoValidate to EnableAllowFocusChange, the cancel button works fine. However, if you press the Save button, the validation is called but the focus then changes to the Save button (bad, becuase now you are past the validation and can press the save button a second time to save and exit with bad data).

    If I set the form's AutoValidate to EnablePreventFocusChange, the validation works fine but you can't press the Cancel button to exit the form; this would appear to override the cancel button's CausesValidation button.

    In Microsoft's documentation section "Validation of Control Data on Windows Forms", I saw the comment "However, in some cases, you might want to allow the user to close the form regardless of whether the values in the controls are valid. You can override validation and close a form that still contains invalid data by creating a handler for the form's Closing event. In the event, set the Cancel property to false. This forces the form to close." So I tried this with both of the the abovementioned AutoValidate settings. With EnableAllowFocusChange it doesn't work correctly. With EnablePreventFocusChange it finally does work to allow exiting of the form at the appropriate time, however the user is still given the validation error message I display in the textbox1_validating event, which is rather annoying and I'd like to avoid this.

    There are a few ways I can think of to get around this such as not using the textbox1_validating event for validations at all, but I'm assuming I'm just missing something important about how Microsoft assumes validations will be written and I'm using the wrong event, overlooking some important validation-related property, etc. How is it assumed I should be validating fields to allow this all to work correctly


  • textbox validating