Pressing Enter key in uiEditField does not call ValueChangedFcn after uiEditField.Value was changed programmatically

32 views (last 30 days)
Inside uiFigure GUI, when you press Enter key with focus on ef = uiEditField, ef.ValueChangedFcn is called.
But this only happens if the ef.Value was changed by typing something in ef.
If I change ef.Value from program, e.g. ef.Value = "new", then pressing Enter key does nothing. It seems the MATLAB does not know the value was changed. I have to type an additional character, and then pressing Enter key works, and calls ef.ValueChanged
Is there a way to let MATLAB know that the value was changed and that it must accept Enter key?

Answers (1)

Ritish Sehgal
Ritish Sehgal on 3 Oct 2022
It is my understanding that you want to trigger ValueChangedFcn callback after setting the uiEditField.Value programmatically.
As per my understanding, these callback functions are only triggered when the value of edit field is changed in the UI, not programmatically.
You can create a wrapper function which sets the value of the edit field and then you can manually call the ValueChangedFcn.
Please refer to the MATLAB answers link below which discusses a similar issue and a sample wrapper function that can be useful to you.
Hope it helps!!
  1 Comment
Tomazz
Tomazz on 3 Oct 2022
Thank you for pointing me to a related problem. However, my problem is a bit different, I think.
I just want that every time user pressed Enter in the uiEditField, ValueChangedFcn would be triggered.
This happens only when the Value was changed from UI, but not when the value was changed programmatically or left unchanged.
To illustrate:
There is a function "logNames.m" in MATLAB documentation for uiEditField:
function logNames
% Create figure and components
fig = uifigure("Position",[100 100 366 400]);
loglist = uitextarea(fig,...
"Position",[134 49 150 277],...
"Editable","off");
namefld = uieditfield(fig,"text",...
"Value", "Bob Langley",...
"Position",[134 367 100 22],...
"ValueChangedFcn",@(namefld,event) nValChanged(namefld,event,loglist));
end
% Create ValueChangedFcn callback
function nValChanged(namefld,event,loglist)
newvalue = event.Value;
previousValue = event.PreviousValue;
loglist.Value = [previousValue; loglist.Value];
end
I'd like to be able to enter the same name twice. Now this is not possible, even if I add and delete a character in uiEditField before pressing Enter.
It seems to me this is not possible to implement within uiEditField, and that I have to program a workaround using WindowKeypressFcn.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!