App not calling EditFieldV​alueChangi​ng(app, event) callback when initial value is empty string.

9 views (last 30 days)
I have a Sudoku app with text boxes (numerical, but it doesn't seem to matter).
I want to control the user input with a ValueChanging callback. These callbacks are working once there's already a value in them, but the callbacks aren't called as the value changes from " " to "5555". I tried using a while loop to make them constantly observe the value of the box, but again it only works if the initial value isn't an empty string...
I don't know much about events, so if there is a way to force the active textbox to call the ValueChanging callback everytime a key is pressed, I think that is a workable solution.
Here is the callback code as it stands now:
function R11ValueChanging(app, event)
changingValue = event.Value;
while (changingValue ~= "" && changingValue ~= "1" && changingValue ~= "2" && changingValue ~= "3" && changingValue ~= "4" ...
&& changingValue ~= "5" && changingValue ~= "6" && changingValue ~= "7" && changingValue ~= "8" && changingValue ~= "9")
app.R11.Value = "";
changingValue = "";
end
end
As I mentioned before, this code does dynamically clear the textbox if something invalid is typed, so in that sense it works, but it just doesn't seem to recognize the ValueChanging event is occurring when the value begins as an empty string.
Edit: I just double-checked something I said, and I found out something weirder. The callback function is actually called. In debug mode, the invalid character gets deleted. But when I don't have any breakpoints, the invalid character stays there.... so now I definitely need help with this...
Here is a video of what's going on: https://youtu.be/SYnlw5GbDAQ

Accepted Answer

Patrick
Patrick on 2 Jul 2024
Edited: Patrick on 2 Jul 2024
It took several hours to try nearly everything, but apparently it's better to just use an automatic Backspace than to assign null values mid-stream. Also, since we are just throwing a backspace, a while loop is too dangerous, so I reverted it back to an if statement.
That same code, but better, looks like:
function R11ValueChanging(app, event)
changingValue = event.Value;
robot = java.awt.Robot;
% This is a good way to clear invalid input live
if (changingValue ~= "" && changingValue ~= "1" && changingValue ~= "2" && changingValue ~= "3" && changingValue ~= "4" ...
&& changingValue ~= "5" && changingValue ~= "6" && changingValue ~= "7" && changingValue ~= "8" && changingValue ~= "9")
robot.keyPress(java.awt.event.KeyEvent.VK_BACK_SPACE);
robot.keyRelease(java.awt.event.KeyEvent.VK_BACK_SPACE);
end
end

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!