I am struggling with a particular issue and it’s because I’m used to how things used to work in FoxPro (code would execute in a very predictable, “linear” fashion unless you use timers). I have a window for searching a list of patients. There are two search fields (Last Name and DOB). I have a method that fires in the LostFocus event of the DOB field which formats the date with a 4-digit year if the user enters only 2 digits…pretty straightforward. However, if the user clicks on the search button without first hitting the tab or enter key to exit the field, the LostFocus event doesn’t seem to be firing and formatting my date until my code executes for the search button.
I guess my real question is this - how do I handle such a scenario? I simply want the code for txtDOB.LostFocus() to complete before the btnSearch.Action() executes so the properly formatted date is being used for the search.
[quote=445366:@Kem Tekinay]The behavior you described is correct, and is predictable. The simplest way to deal with it is to have your button manually shift focus.
self.Focus = nil // Will fire the LostFocus event of the current control
[/quote]
are you suggesting that go in the ACTION of the BUTTON?
if so… that doesn’t work… I just tried it
Within a window or Container Control, Self refers to the immediate window or Container Control. Me refers to the control itself. Outside a window or Container Control, Self and Me are interchangeable and refer to the instance of the object in which the code resides.
None of these suggestions work for the 2nd and subsequent clicks even if you click and type into the original text field.
Try this in your button:
[code]#If TargetMacOS Then 'This works as expected on windows so you dont need to do this
dim OldFocus as rectcontrol = Self.Focus 'remember the control that had focus
'set focus to the button
self.focus = me #EndIf
'Do your button stuff
System.DebugLog(CurrentMethodName)
#If TargetMacOS Then 'This works as expected on windows so you dont need to do this
'move focus back to the control
self.focus = OldFocus #EndIf[/code]
[quote=445365:@Aaron Schacht]I am struggling with a particular issue and it’s because I’m used to how things used to work in FoxPro (code would execute in a very predictable, “linear” fashion unless you use timers). I have a window for searching a list of patients. There are two search fields (Last Name and DOB). I have a method that fires in the LostFocus event of the DOB field which formats the date with a 4-digit year if the user enters only 2 digits…pretty straightforward. However, if the user clicks on the search button without first hitting the tab or enter key to exit the field, the LostFocus event doesn’t seem to be firing and formatting my date until my code executes for the search button.
I guess my real question is this - how do I handle such a scenario? I simply want the code for txtDOB.LostFocus() to complete before the btnSearch.Action() executes so the properly formatted date is being used for the search.
Thank you![/quote]
dont use lost focus alone as not everything causes a control to lose focus (as you’ve discovered)
write a single method on the form that both the lost focus event and the buttons action event can call passing the current contents of the field to it to use for searching
you dont have to manually FORCE the field to lose focus which can cause other weird effects in a user interface
@Dave S Thank you, your solution worked perfectly and consistently. I also appreciate everyone else’s suggestions. In this particular case, Dave’s solution works and doesn’t produce any unanticipated anomalous behaviours.