Text Box Validation

  • Thread starter Thread starter daudi
  • Start date Start date
D

daudi

I have a text box which i validate at LostFocus Event. My problem is
that if user click at cancel it also validates this thex box and
brings a error message. I do go about it in that when it is cancel
button click then it should not validate the text box and instead
unload the form.
Thanks
 
Check out the validation event for the textbox and the
CausesValidation property of the cancel button.

Thanks,

Seth Rowe
 
Actually, there isn't much code involved, all you need to do is move
the code you have in the LostFocus event to the validating event. That
gives you access to the the ValidatingEventArgs object which has a
cancel property. So if the text isn't valid then just type e.Cancel =
True and then the program will automagically take care of the rest
(preventing focus from changing). After that is set, all (I think)
standard controls have a CausesValidation property that is normally
set to true. When focus changes to a new control, if this property is
set to true it fires the Validating event for the control who had the
focus last. So setting this to false (for your cancel button) will
stop the validating event from firing when you click on the button.

Hope that makes sense

Thanks,

Seth Rowe
 
thanks alot

daudi
Actually, there isn't much code involved, all you need to do is move
the code you have in the LostFocus event to the validating event. That
gives you access to the the ValidatingEventArgs object which has a
cancel property. So if the text isn't valid then just type e.Cancel =
True and then the program will automagically take care of the rest
(preventing focus from changing). After that is set, all (I think)
standard controls have a CausesValidation property that is normally
set to true. When focus changes to a new control, if this property is
set to true it fires the Validating event for the control who had the
focus last. So setting this to false (for your cancel button) will
stop the validating event from firing when you click on the button.

Hope that makes sense

Thanks,

Seth Rowe
 
Back
Top