Code Execution Order Behaviour

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!

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

but THIS DOES

self.SetFocus

Hmm, I tried it too before posting. Wonder what’s different?

in the BUTTON ACTION … self is the BUTTON, not the textfield, and button has no knowledge of who had focus previously

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]

  1. dont use lost focus alone as not everything causes a control to lose focus (as you’ve discovered)
  2. 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
  3. you dont have to manually FORCE the field to lose focus which can cause other weird effects in a user interface

What Norman said. You can also use a flag that can be updated in the TextChange event to determine whether then field needs updating at all.

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

Aaron, just out of interest, what version of macos and xojo are you using?

um no
in a button on a layout SELF is the LAYOUT
me is the button

https://documentation.xojo.com/getting_started/object-oriented_programming/me_vs._self.html

@JulianS macOS 10.14.5 and Xojo 2019.1

Don’t you have this issue?

https://www.dropbox.com/s/e2m5cwhg78mywkj/LostFocusMac.mp4?dl=0

Wow, Julian. I never knew of that issue. Thanks for pointing it out!

Julian - thank you for posting that video! So, is this “issue” is a Xojo bug? Seems to be.