ListBox MultiSelect simulate always with Ctrl modifier?

6 views (last 30 days)
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?

Accepted Answer

Jonas
Jonas on 13 Feb 2023
Edited: Jonas on 16 Feb 2023
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

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!