Firemonkey TNumberBox - new value not available until losing focus

1.1k Views Asked by At

Using a TNumberBox control (at least in Windows, probably other platforms too), when I type in a new value, and then press a button (TButton) to save the changes, upon reading the TNumberBox.Value property, it's returning the original value before the edit was made. It turns out, this value is not accessible until after the control loses focus.

Now the button used to save changes, I have deliberately disabled its CanFocus property, along with all buttons in my app. This is because with the style I use (Jet), a focused button looks horrible (black text on dark gray background). Not to mention, on mobile platforms, focus is practically useless in my case.

Otherwise, if I don't disable CanFocus on this save button, the focus is properly taken off of the TNumberBox and its Value property is okay. But, when disabling CanFocus on the save button, the focus is never taken off of this control, and therefore reading its Value property returns the old value.

How can I ensure the Value property returns the correct new value without changing the save button to CanFocus?

Enabling KillFocusOnReturn is not useful, because users would have to be aware they need to press "Return" or "Enter" which is not an option.

NOTE

This problem also persists if you were to utilize the TNumberBox.OnChangeTracking event. I'm not sure how this event would be useful if you can't even read the new value.

2

There are 2 best solutions below

0
On BEST ANSWER

I just discovered a dirty work-around at least. Enable the button's CanFocus property, then on button click, the very first thing that happens should be setting the focus back to the TNumberBox control. That would both allow focus to be taken off of the control (thus refreshing the value), while also taking the focus away from the button (thus avoiding the terrible look).

This however would not be suitable if one were to be utilizing TNumberBox.OnChangeTracking and reading the Value from within there.

6
On

I discovered that FireMonkey needs <ENTER>, or a loss of focus, to accept a value in any sort of edit. I don't think you actually need to kill the focus on return.

The real solution to this would seem to be editing the style to change the display of focused buttons. I didn't like the display of certain controls, or I wanted to add a graphic, etc., so I just went into the style in my project and made changes to get what I wanted.