Validating Textboxes

I am trying to Validate a group of textboxes on a form.  I would like it to work where the validation occurs as user is leaving the textbox and the focus is back to the textbox to correct.  This works well in the validating event EXCEPT I have not provided a way to break out of the loop caused by the error messages.  For example, if the user makes an invalid entry in a textbox and attempts to tab to the next textbox  The error appears and focused back to the textbox.  Now suppose that the user would like to cancel the operation and do something else or start over.  In this case, the user has no way of doing something else and must correct the textbox.  Maybe I have the wrong event or properties set properly?  Maybe I need to programtically provide a way to cancel?

Can someone provide something that works and also state how the textbox properties should be set.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

ElrondCTConnect With a Mentor Commented:
I start my Validating event handler with a statement like:

        If btnCancel.ContainsFocus Then
        End If
where btnCancel is the button the user can click to cancel data entry. Note that this will still catch the user if they just try to close the form by clicking on the red X, but that's intentional; I want them to explicitly decide that they're abandoning their input by canceling their changes.
Joel CoehoornConnect With a Mentor Director of Information TechnologyCommented:
If you validate the previous TextBox when the next Textbox gains focus, you'll skip that particular problem.  However, you're still committing the sin of breaking the user's natural flow, which may sometimes be different than your predicted tab order.  Instead, use an error provider component to mark invalid Textboxes on LostFocus() as you progress and build the validations in such a way that you re-validate the entire form before moving on.
garyinmiami2003Author Commented:

Can you give me an example to make it easier for me to understand?  Pick an event (like validating)
Put a simple edit  in and show how to use (set ) the error provider component.  Show any dims, state any property values that must be set.  I will award the points just would like to be able to use your solution
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Could you just make it so "Null" or EmptyString is a valid option in the text box?
Then you could tell the user (via a status label or tooltip) that they need to clear the field if they wish to continue.

Of course, you could just wipe the field for them at the end of your validatation also.. but sometimes thats not always a good thing to do.

garyinmiami2003Author Commented:

I don't see your proposed solution as a viable option.  To elaborate on my problem and what I tried:  
I tried placing the edits in the validating event.  This worked ok when going from one textbox to another but if they wanted to end the screen or cancel it would force the validation no matter what and require a valid entry.  No good!
  With the validation event, I have the problem of getting stuck in the validation error messages.  I am looking for someone to tell me a good way to force the validation but give the user a chance to cancel out of it and do something else, or start over.  
Joel CoehoornDirector of Information TechnologyCommented:
Textbox controls have GotFocus() and LostFocus() events, which I presume you were handling to do your validation.  Instead of handling LostFocus() to validate a textbox, you could instead use the GotFocus() event for the next textbox in your tab order.  That would fix the problem you complained about, and prevent your validation from running if the user goes back to a previous step.  The problem is, if the user uses the mouse to navigate your form out of order things won't work as they expect.

It's better not to grab the focus from the user.  Give them immediate feedback, sure, but don't grab the focus.  .Net includes an ErrorProvider component for marking fields when you deem them not valid, so the user can see where the errors are.  You can use this component instead of stealing focus.
Joel CoehoornDirector of Information TechnologyCommented:
Ah, you validate everything in one place for the whole form?
garyinmiami2003Author Commented:
I split the points because both experts who received points contributed to the solution.  jcoehoorn and  ElrondCT.  
Well, I do some validation for the whole form when multiple items are interconnected (a specific answer in one control only permits certain answers in another), before saving everything to the dataset/database. But I also have validating events for the individual controls, and that's where I put the cancel code that I showed, so the individual control doesn't try to validate its entry if the user is bailing out.
All Courses

From novice to tech pro — start learning today.