필터 지우기
필터 지우기

Listbox is ignoring command to reset ListboxTop

조회 수: 5 (최근 30일)
AstroGuy1984
AstroGuy1984 2017년 4월 24일
댓글: Corey 2020년 9월 18일
I am writing a GUI (WITHOUT GUIDE) using 2016A. One element in this GUI is a uicontrol listbox in which the user can select multiple elements. This listbox is created within a panel like so:
uicontrol('Parent', selSeries, 'Style', 'listbox', 'Min', 0, 'Max', 1e3,...
'FontSize', 8, 'units', 'normalized', 'Position', [0.02, 0.02, 0.94, 0.94],...
'Tag', 'selectSeriesChoiceBox', 'Callback', @selectSeries_Callback)
Where selSeries is a panel within a uitab object within a uitabgroup which resides in the figure.
The first thing I do within the callback is to invoke the "old" data selection and using some logic, reference it with the "new" selections. The purpose of this is to allow the user to just click down the list like a checklist, without having to use CTRL or SHIFT to make multiple selections. I use get/setappdata to accomplish this, and the behavior is as I desire.
When I set the new values, however, MATLAB will (as designed) automatically change the position of the scrollbar. I wish to retain the user's scrollbar position. So I try to update the property 'ListboxTop' after I set the new listbox values. This processes without error, but it is ignored.
function selectSeries_Callback(hObject, event)
% LOGIC TO DETERMINE NEW VALUES VECTOR - newValue
userTop = hObject.ListboxTop;
hObject.Value = newValue;
hObject.ListboxTop = userTop;
% OTHER CODE
end
I have also tried the method of setting the properties using set(hObject, 'ListboxTop', userTop).
Adding to the mystery is the behavior in debug. If I again try to set the property within the shell, the listbox again ignores me without error. HOWEVER, if I were to pull all properties in the shell, (e.g. get(hObject) or clicking "all properties") and THEN setting the property, it worked.
This gave me the idea for a quick workaround to try doing that in the code as such:
function selectSeries_Callback(hObject, event)
% LOGIC TO DETERMINE NEW VALUES
userTop = hObject.ListboxTop;
hObject.Value = newValue;
tmp = get(hObject);
hObject.ListboxTop = userTop;
% OTHER CODE
end
This did not solve my issue. The listbox again remains at the bottom position and doesn't reset to where I am asking it to. However, if I set a breakpoint at tmp, and then F10 through those two lines, it works!
This implies to me that there's something subtle I'm missing with the activity of the object. Maybe something is getting loaded (or some such) when the breakpoint is hit? I've also tried putting in a pause to see if maybe something needed to be caught up.
Any insights that could be provided to fix this would be greatly appreciated. Thanks!
  댓글 수: 4
Melvin
Melvin 2018년 10월 25일
I have the same problem in 2017a. It's not scrolling down by setting ListboxTop to the last element. Hope this will get fixed soon.
Corey
Corey 2020년 4월 9일
i have the same issue still with matlab r2019b. i am keeping track of what the user has already selected without them having to hold down control and if i set the listboxtop before leaving the callback function it does not work unless i step through the program.

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

답변 (2개)

Prashant Arora
Prashant Arora 2017년 4월 27일
Hi,
This seems to work for me. I created a uicontrol Listbox, but inside a figure.
h = uicontrol('Parent', gcf, 'Style', 'listbox', 'Min', 0, 'Max', 1e3,...
'FontSize', 8, 'units', 'normalized', 'Position', [0.02, 0.02, 0.94, 0.94],...
'Tag', 'selectSeriesChoiceBox')
%Add some Strings
h.String = repmat({'Hello';'How';'Are';'You'},5);
Then I added the callback as follows
function selectSeriesChoiceBox(object,event)
newValue = object.Value + 1;
userTop = object.ListboxTop;
object.Value = newValue;
object.ListboxTop = userTop+1;
end
One thing which you can possibly try is using a "drawnow" command. This will update the MATLAB figure and maybe that's what's missing.

Michael Daenen
Michael Daenen 2020년 9월 18일
편집: Michael Daenen 2020년 9월 18일
I found a workaround for this by adding two extra commands.
h = uicontrol('Style','list',...);
pause(0.1);
get(h,'ListboxTop');
set(h,'ListboxTop',1);
  댓글 수: 1
Corey
Corey 2020년 9월 18일
I think the key to this workaround is requesting the top (and throwing the data away) using get immediately before setting it.
Also consider using drawnow instead of pause for slightly quicker execution.

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by