ValueChanged event not fired on uieditfield when setting property Value

조회 수: 34 (최근 30일)
Andreas Klotzek
Andreas Klotzek 2019년 1월 10일
댓글: Rob Campbell 2022년 12월 22일
Hi
I have a uieditfield:
u = uieditfield();
The I attach a listener to the ValueChangedFcn
u.ValueChangedFcn = @(s,e)disp(e);
If I type into the edit field the my listener gets called as expected. But if I set the property directly
u.Value = '2';
no event is fired.
Is there something wrong with what I am doing or with Matlab?
  댓글 수: 5
J. Alex Lee
J. Alex Lee 2020년 4월 24일
Is this a concious decision based on some programming principle? It seems to me the more obvious/expected behavior is that changing the "Value", no matter how, triggers the "ValueChangedFcn"...otherwise they should have called it the "UIElementManipulatedFcn"...
I understand the wrapper concept as suggested by Adam and demonstrated by Jan, but it seems a cumbersome solution especially if you want to ensure your event object looks the same as it does from the manipulation of the ui element, and you want to do this for many types of ui elements...
Am I missing something?
Rob Campbell
Rob Campbell 2022년 12월 22일
I agree. This is weird and differs from how this works in a Figure vs a UI.

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

답변 (2개)

Jan
Jan 2019년 1월 11일
편집: Jan 2019년 1월 11일
You can write a wrapper for setting the value programmatically, which calls the callback manually:
function SetMyValue(objectH, Value)
objectH.Value = Value;
myEvent.Origin = 'Value set programmatically';
objectH.ValueChangedFcn(objectH, myEvent);
end
  댓글 수: 9
Walter Roberson
Walter Roberson 2022년 10월 8일
"however, it doesn't prevent an unaware user from programmatically changing a UI component property value without an appropriate set/callback method being executed"
A "user" who has access to **programmatically* change the ui component property values is a developer, so you are effectively asking how one developer can prevent a different developer from taking particular actions with the class that the second developer has source-code-modification access to.
If you want to divide an application into two parts, one modifiable by original developers and the second modifiable by less knowledgeable secondary developers, then put access restrictions on properties so that the secondary developers have to go through an interface to set the properties and the interface ensures that the necessary steps are taken to ensure consistency.
shakedpa
shakedpa 2022년 10월 10일
The original suggestion by Jan works fine, so that's ok for me. I felt there was a "more correct" way of doing it, but if there isn't anything obvious - nevermind.

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


Alexander Cranney
Alexander Cranney 2021년 11월 15일
편집: Alexander Cranney 2021년 11월 15일
Another way to get the behavior you want is (mis)use a test case. Test cases have a number of methods for modifying GUI values in ways that mimic having a user manipulate the values, which fires all of the ChangedFcns. In this case, you can use the "type" method:
u = uieditfield();
u.ValueChangedFcn = @(s,e)disp(e);
fakeTestCase = matlab.uitest.TestCase.forInteractiveUse;
fakeTestCase.type(u,'2')
  댓글 수: 1
Alexander Cranney
Alexander Cranney 2021년 11월 15일
Ah, looks like you can do it in a single line without instantiating "fakeTestCase":
matlab.uitest.TestCase.forInteractiveUse.type(u,'2');

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

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by