Richard Brennan

Richard Brennan

  • NA
  • 114
  • 85.6k

Im using the leave event of a text box

Apr 13 2011 9:56 AM
I am using the leave event of a text box to validate a username and password - the problem is if the user has started to enter data in the text box and they decide to press the cancel button - the leave event on the text box does not need calling. Is there a way to stop the leave event happening if the cancel button is pressed. can I use the 'try and catch' thing and check if the click event on cancel has happened? If so can some on explain try catch as i have never used it.  Like what is optional and compulsory and how the logic flows through it?


---------------------
Solved it like this

if (!btnChangePasswordCancel.Focused) [this is inside the leave event]
  dont validate the text

Answers (7)

0
Mayur  Gujrathi

Mayur Gujrathi

  • 389
  • 3.9k
  • 1m
Apr 15 2011 9:15 AM
Try this
under click event of cancel button do like this
canclebutton.CausesValidation=false

due to this cancel button executes before doing any validations
you can also set this property by right clicking on button and set cause validation to false
0
Vulpes

Vulpes

  • 0
  • 96k
  • 2.6m
Apr 15 2011 6:50 AM
Well, the TextBox control actually has a Validating event which you're supposed to handle for validation so it's not necessary to derive a custom TextBox control to do this. However, there are some issues about setting focus from this eventhandler.

Consequently, for username / password validation, I prefer to do the two together in the logon button's click handler from where I can clear both boxes and set the focus back to the username box if the validation fails.
0
Sam Hobbs

Sam Hobbs

  • 54
  • 29.3k
  • 2.1m
Apr 15 2011 2:29 AM
This is very interesting.

In C++ there is something called MFC. MFC has a very sophisticated ability to perform validation. Experienced MFC developers would insist that it is very poor design to validate fields when an Okay button is clicked.

I am neutral. It does not matter to me; both ways can be useful, depending on requirements. It is interesting that people can be so adamant (demanding) that one of the two options are the only good way.

Except I will say this much. Microsoft C# programmers seem less object-oriented. They don't use derivation as much as Microsoft MFC programmers do. An object-oriented solution to validation would be done by deriving a control from a control and do the validation in the derived control. That would be entirely possible if the validation did not include other controls. Validation logic such as what MFC has would include logic to avoid validation when the form is canceled. I am surprised that .Net does not.
0
Suthish Nair

Suthish Nair

  • 0
  • 30.5k
  • 7.2m
Apr 14 2011 2:46 PM
Its a bad logic to validate user credentials on textbox leave event. Normally websites don't have this kind of implementation.
0
Vulpes

Vulpes

  • 0
  • 96k
  • 2.6m
Apr 13 2011 11:09 AM
That's fair enough if it's a 'change password' dialog.

I thought when I read the question that it was just a normal 'logon' dialog though I see from the addition to your original post that you've figured out a way to avoid validation in the Leave event in any case, if the user presses the cancel button.
0
Richard Brennan

Richard Brennan

  • 0
  • 114
  • 85.6k
Apr 13 2011 11:02 AM
the username and password are being entered on a change password form - i thought if it is validate as they finish typing they wont get annoyed by entering username, password, new password and cofirm password - only to be told the username (first entry) was invalid - so annoying.

But i have done what u said on the normal logon form.
0
Vulpes

Vulpes

  • 0
  • 96k
  • 2.6m
Apr 13 2011 10:16 AM
If you're validating username and password together, it would be better to validate them in the Login button's Click handler and, if validation fails, you can  empty the textboxes and put the focus back to the username textbox. If the Cancel button is pressed, that's not a problem either.

This isn't a good use for try/catch which is designed to catch what would otherwise be runtime exceptions - not validation errors of this nature.