ListBox MultiSelect simulate always with Ctrl modifier?

조회 수: 4 (최근 30일)
Jonas
Jonas 2023년 2월 13일
편집: Jonas 2023년 2월 16일
dear community,
can someone check the ListBox MultiSelect Example from the documentation for me? It does not work for me, I can only select one item, not multiple at once
function multiselect
fig = uifigure('Position',[100 100 350 275]);
% Create Text Area
txt = uitextarea(fig,...
'Position',[125 80 100 50]);
% Create List Box
lbox = uilistbox(fig,...
'Position',[125 150 100 78],...
'Multiselect','on',...
'ValueChangedFcn',@selectionChanged);
% ValueChangedFcn callback
function selectionChanged(src,event)
txt.Value = src.Value;
end
end
best regards
EDIT:
I noticed that I Have to press Ctrl for MultiSelect. How unconvenient.
Is there a way to modify inputs such that they are interpreted always as ctrl+left click?

채택된 답변

Jonas
Jonas 2023년 2월 13일
편집: Jonas 2023년 2월 16일
I tried to circumvent the ctrl modofier in such a way, that I look into the event's previous value and delete/save as necessary. Now, there is also always one option selected
I modified the callback as follows:
function selectionChanged(src,evt)
prevVal=evt.PreviousValue;
currVal=src.Value;
if strcmp(fig.SelectionType,'normal') % to preserve ctrl behavior
isThere=ismember(prevVal,currVal);
if any(isThere)
prevVal=prevVal(~isThere);
else
prevVal=[prevVal currVal];
end
src.Value=prevVal;
end
end
please note that with the current state, the bahavior with ctrl left click changes and does not work as intended anymore.
EDIT: edited code to properly preserve ctrl click behavior

추가 답변 (0개)

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by