ListBox MultiSelect simulate always with Ctrl modifier?

4 Ansichten (letzte 30 Tage)
Jonas
Jonas am 13 Feb. 2023
Bearbeitet: Jonas am 16 Feb. 2023
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?

Akzeptierte Antwort

Jonas
Jonas am 13 Feb. 2023
Bearbeitet: Jonas am 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

Weitere Antworten (0)

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by