Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cancel form before validating textbox?

Status
Not open for further replies.

Tangleman

Programmer
Sep 18, 2003
8
US
I am writing a GUI that requires validating a textbox when it loses focus. For that I am using the Validating Event Handler. However, if the user clicks the cancel button on the form, I need to prevent that textbox's validation event from firing.

I prefer to use the validating event, since that would validate the data the moment the user tabs out of the textbox.

I looked up info on the situation and found that the textbox's validating event gets fired before the cancel button's Enter event gets fired.

I need an answer to this rather soon, so if anyone can offer any advice, I would greatly appreciate it! Thanks!
How can I make sure it is the cancel button being hit before I validate, so that I will know whether I need to validate the text in the textbox or not?
 
Hi,

Why don't you just create a form level boolean variable something like 'isCancelPressed' and when the user clicks Cancel button, in the clicked event make 'isCancelPressed' to true and then in the validate event check if 'isCancelPressed' is TRUE, if so don't validate, just close the window else validate and give error message.

-Kris11
 
Just tried it, and it didn't work. The cancel button's click event doesn't even get fired until after the validation event has been handled.

Is there some type of event stack available that I could look through to see which event have yet to be fired? If there is, I could look ahead to see if Cancel's click event is in there.
 
The problem is that the Validating event fires before the Click event of the button, and therefore setting a boolean in the click event will be too late.

How about not setting
Code:
e.Cancel = True
in the Validating event, and then checking the error provider for errors in the OK (or non-Cancel) button click event ?

For example:
Code:
Private Sub OKButton_Click(sender As Object, e As EventArgs) Handles OKButton.Click

    If ErrorProvider1.GetError(TextBox1) <> String.Empty Then

        'Not valid - message box to tell user ?

    Else

        'Valid - do OK task

    End If

End Sub
 
Got it figured out. The Errorprovider thing didn't do exactly what I was looking for, thanks though. In the validating event, I set a formwide boolean true when bad data appeared and set a formwide string to the name property of the offending textbox. Then I redirected all textboxes and the two buttons' Enter event to the same handler. In that handler, if the sending object is the Cancel button, I clear the badData flag, close the form, and exit the sub. The only other possible sending objects are textboxes and the OK button. If the badData flag is set, I pop up the MessageBox with the choices OK and cancel. I loop through the form's controls, isolating the textboxes and then, based on the result from the MessageBox, I either reselect the offending text or do something else, and clear the badData flag of course. Lastly, if the sending object was a button, I call the OK.PerformClick method, since the only way a button could've survived the code is if it is the OK button.

Thanks for all your suggestions, though.
 
Correction. Still not figured out. I took out the last OK.PerformClick because it was clicking OK even when I tabbed to it. I solved most of the problems, but now when I click the OK button, it validates the textbox, pops up a messagebox, and if I click OK (meaning that I want the value out-of-range), it just goes back to the form without actually performing the OK.click. It's almost like it forgot that it still had to execute the Ok.click. Any idea why this happens?

Also, I was thinking a way around it would be if I could somehow look at a stack or list of events yet-to-be-handled, and if the OK.click is in that list, then I can tell it to execute the OK.click at the end. Does such a list or stack exist? And if so, I can I access it?

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top