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

조회 수: 10 (최근 30일)
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?

답변 (1개)

Ritish Sehgal
Ritish Sehgal 2022년 10월 3일
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
Tomazz
Tomazz 2022년 10월 3일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by