Validating event of textbox
I have a textbox with a validating event listener to validate the content of the textbox against a regular expression.After the validation, if entered value is not proper,i am showing the messagebox and i am cancelling the event so that mouse cursor move back to the textbox which has improper value.The problem is, when I click the X button on the right top corner of the form, I don't want the validation to be fired because I am closing the form anyway. A control event list is accessed by clicking this control's property sheet’s event (lightning looking) toolbar button.You can then enter the code in the automatically generated handler with a name combining both the name of the control and '_Validating'.Any attempt to do so through a Form Closing handler will not work because it is then too late once execution reaches such handler.However, the click of the "X" button can be captured promptly via overriding the Wnd Proc() method and testing for a 'm. When that condition is true, the previously introduced 'Auto Validate = Auto Validate. Error Text = "" End If End Sub I came here in search of a simple method to cause a form to close when a Validating event handler raises an exception, reports it, and needs to force the form to close.
As said by MSDN, When you change the focus by using the keyboard (TAB, SHIFT TAB, and so on), by calling the Select or Select Next Control methods, or by setting the Container Control.That is not so for the Form Closing events that will all execute after validating events.That is why that validation cannot be disabled from any of these Form Closing events.You should be able to copy and paste that method as is in your form's class. Cancel = True Msg Box(tag Desc & " must match pattern: " & regex Desc, Msg Box Style. Conversely, a normal Close event receives a Form Closing Event Args. Since the Close method on a form takes no arguments, there is no direct way to force the issue, giving rise to the need for a hack.At this point, both the 'Cancel' an "X" buttons should cancel valdations. This article discusses a number of such hacks, but I think mine is much simpler to implement.