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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My Valid or LostFocus Dosn't work

Tips -N- Tricks

My Valid or LostFocus Dosn't work

by  danceman  Posted    (Edited  )
Say your user is entering a new record and is sitting on a field that requires validation. After entering bad data for that field, but before moving focus to another field, the user clicks the Save button on your toolbar or chooses Save from the menu. Whooshùthe data is saved, including the bad data. What happened?

In VFP, as in FoxPro 2.x, the Valid routine for a text field doesn't execute until focus leaves that field. Clicking a button on a toolbar or picking a menu item doesn't change focus and therefore doesn't fire the Valid method or the LostFocus method, of course. (In fact, a toolbar never has focus, which lets you do pretty cool things.)

Why does it behave this way? Because we want it to. It feels wrong in this situation, but suppose the toolbar or menu item the user chose was Select All or Paste. We sure wouldn't want the focus to change (and the Valid and LostFocus methods to fire) in that case.

So how do we make sure the data gets validated? Simpleùmake sure focus changes. One way to do this is to reset focus to the same field:

_SCREEN.ActiveForm.ActiveControl.SetFocus()

Since menus and toolbars don't get the focus, _SCREEN.ActiveForm is still the same form as before. We just set the focus back to the object that had it, which triggers that control's Valid, LostFocus, When and GotFocus methods.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top