UI logic for committing objects using FocusLost event

I have been using FocusLost on objects (eg textFields, textAreas, ListboxCells etc) to determine when because a different object has taken the focus, then the content of the previous object should be committed (to database or whatever).

This works OK when moving around within the app, but doesn’t work if selecting a different app, or when trying to debug as the focusLost cannot discern between another object in this app, or a different app. So, for example, if you are debugging and hit a break point, the object will lose focus and commit its data - not what you intend at all.

My solution is to create a property (app.isActive as boolean) which is set by the application.activated/deactivated events.

These events are triggered after the focusLost event so focusLost calls a timer.callLater(0, on_focusLost) to test if the app is active before committing the content of the object.

Now the objects only commit when you want them to. You could take this further and add the same logic to a subclass of the objects - with a new event “FocusLost_noReally_exclamation”.

LostFocus is not a reliable place to decide if info has changed. It either doesn’t fire when you need it to, or it fires to often. I had to create a rather elaborate mechanism to handle this scenario. There’s nothing built in to Xojo that will handle this 100% of the time. That’s not an indictment of Xojo, but of the underlying OS.

1 Like