필터 지우기
필터 지우기

Listbox multi selection without using Ctrl?

조회 수: 5 (최근 30일)
Jurgens Wolfaardt
Jurgens Wolfaardt 2017년 12월 11일
댓글: Walter Roberson 2023년 11월 25일
Hi All
I have a GUIDE listbox in which I select and deselect multiple options regularly. Listbox entry selection unfortunately works like Windows file selection in Explorer; You have to hold down Ctrl if you want to deselect only one file in a selection or want to add one. Can I set the listbox to rather toggle clicked entries? When my customer forgets to hold down Ctrl, all their selections disappear...
Thanks!

채택된 답변

Walter Roberson
Walter Roberson 2017년 12월 21일
Each time they make a selection, the Callback is invoked, and the control's value property is updated to reflect the current selection.
If you keep track of the previous value property then if the new value is scalar (because the user forgot to hold control) then check to see if the new value was present in the previous selection: if it was then update the value property to the old value without the current value, because this is the case where a user clicked on an existing selected value intending to exclude it. If the new value was not present in the previous value then set the new value to be the combination of the old one and the current value, as this is the case of the user clicking on a nonselected value intending to select it.
You might not want to apply this logic if the previous value was scalar and the new one is as well, as that is the ambiguous case: it could be that the user had selected one thing before and clicked something else because they changed their mind, or it could be the case where the user had one thing selected and forgot to hold down control to select the second one.
I recall that I implemented this logic at some point, but my memory of when has gone into hiding so I would have difficulty digging it up.

추가 답변 (2개)

Amy
Amy 2017년 12월 20일
편집: Amy 2017년 12월 21일
Have you considered putting multiple checkboxes or toggle buttons inside of a button group instead? It sounds like that might be closer to the behavior you're describing.
  댓글 수: 3
Rohit Deshmukh
Rohit Deshmukh 2019년 12월 12일
@Jurgens-
I am looking for same solution. How did you implement radio buttons in list box?
Walter Roberson
Walter Roberson 2019년 12월 12일
You cannot have a radio button inside a list box. You would use a uibuttongroup
Also note that by definition a radio button group has exactly one selection (or sometimes none) which would be the opposite of the requirement of the Question which inherently requires multiple selection.

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


Harry Weston
Harry Weston 2023년 11월 23일
편집: Walter Roberson 2023년 11월 25일
I got the following to work for selecting multiple values on an app listbox:
% check if the value is already selected and remove if it has
if max(contains(app.selectedYChannels, app.YChannelsListBox.Value))
% remove
valToRemove = contains(app.selectedYChannels, app.YChannelsListBox.Value);
app.selectedYChannels{1,valToRemove} = [];
app.selectedYChannels = app.selectedYChannels(~cellfun('isempty',app.selectedYChannels));
else
% save stored selection
app.selectedYChannels = [app.selectedYChannels, app.YChannelsListBox.Value];
end % if
% update Y Channel/s list box
app.YChannelsListBox.Value = app.selectedYChannels;
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 11월 25일
I would suggest that it would be clearer if you changed
if max(contains(app.selectedYChannels, app.YChannelsListBox.Value))
to
if any(contains(app.selectedYChannels, app.YChannelsListBox.Value))

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

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by