ListBox MultiSelect simulate always with Ctrl modifier?

3 次查看(过去 30 天)
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