Link to home
Start Free TrialLog in
Avatar of garyinmiami2003
garyinmiami2003Flag for United States of America

asked on

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.
   
SOLUTION
Avatar of Joel Coehoorn
Joel Coehoorn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of garyinmiami2003

ASKER

jcoehoorn:

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
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.

ScottParker:

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.  
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah, you validate everything in one place for the whole form?
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.