cancel validation of txtbox if form close btn(x) is pressed
Is there an way to supress the validating event of textbox when the forms close button(X) is pressed.
In the textbox validating event , if the condition fails then i am displaying an messagebox. Now suppose if the textbox has the focus and user directly presses the forms X button , then the messagebox is displayed. I want to supress this.
Plz Help!
Re: cancel validation of txtbox if form close btn(x) is pressed
What you describe is not the default behaviour. The only times that the Validating event is raised is when the user attempts to shift focus from the control or when the code calls Validate or ValidateChildren. Clicking the Close button on the form's title bar does not shift focus from the active control so, unless there's something broken on your system, you must be calling Validate or ValidateChildren on the FormClosing or FormClosed event. You don't have to suppress anything; you just have to not initiate it.
If you don't it's your code then try it with a brand new project where that's the only functionality and see how that behaves.
Re: cancel validation of txtbox if form close btn(x) is pressed
Well infact it behaves.
i have created a new project and all it contains is an textbox and the following code
vb Code:
Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If Not IsNumeric(Me.TextBox1.Text) Then
MessageBox.Show("Invalid")
e.Cancel = True
Else
e.Cancel = False
End If
when the project runs the textbox has the control, i entered some non numeric string in it an pressed the X button, i got the msgbox
Re: cancel validation of txtbox if form close btn(x) is pressed
In my original test I displayed the form by calling ShowDialog. I just tested again calling ShowDialog and Show. When I called Show it did behave as you describe. When you close a form displayed with ShowDialog, you are actually hiding it. When you close a form displayed with Show, you are actually closing it. In that case, the form's Validate method gets called and the Validating event will be raised.
I just had a quick look and couldn't see an obvious check to make in the Validating event. A more thorough examination may reveal more but the only thing I can see for the moment is trapping the WM_CLOSE message in the WndProc method and setting a flag. I guess the first thing to ask is whether or not this form is your startup form and, if it's not, is it practical to display it by calling ShowDialog?
Re: cancel validation of txtbox if form close btn(x) is pressed
Thanks and Yes its not my main form and i can manage with showdialog.
but for the brain size sake
Quote:
Originally Posted by
jmcilhinney
When you close a form displayed with Show, you are actually closing it. In that case, the form's Validate method gets called and the Validating event will be raised.
what happens in the forms validating event that makes the textbox to raise its validating event.
Re: cancel validation of txtbox if form close btn(x) is pressed
It's not the form's Validating event. The form's Validate method is called from, if I remember correctly, the WndProc method, which would occur when the WM_CLOSE message is received. I'm not at a computer with VS installed right now so I can't check but you can set a breakpoint in the TextBox's Validating event handler and check the Call Stack window yourself.
Re: cancel validation of txtbox if form close btn(x) is pressed
That's an interesting thread, I had a very similar problem. While it could be solved for the X-button, I still haven't found any solution for an additional Cancel-Button:
Quote:
Clicking the Close button on the form's title bar does not shift focus from the active control
Any other button, however, does. Meaning, if I enter a textbox which has validation implemented, clicking the Cancel-Button (the one I added, not the X-button) won't do anything, because the focus is stuck to the textbox. Is there any solution to that? How can you make your custom button behave like the x-button?
Thank you very much for any hints!
- spitfire
Re: cancel validation of txtbox if form close btn(x) is pressed
I just stumbled over a solution by myself: Simply set the CausesValidation-Property of the button to false, and it can do its thing :)