Enable / Disable specific listbox item

14 ビュー (過去 30 日間)
Dan
Dan 2014 年 10 月 13 日
編集済み: Amos 2023 年 4 月 18 日
Is there a way to disable an item in a listbox. Basically I want to have a listbox that displays a number of available items for selection but want a subset of these items to be greyed out (disabled).
I can (of course) just check for the forbidden item AFTER the user has selected it and tell him that this item is unavailable. But that seems in elegant. Alternatively, I could simply not list forbidden items to begin with but.... also inelegant.
Ideas?
Thanks!
Dan

採用された回答

Image Analyst
Image Analyst 2014 年 10 月 13 日
You can't do that. The workarounds are the two you listed.

その他の回答 (2 件)

Robert Cumming
Robert Cumming 2014 年 10 月 13 日
By using this FEX you can make it look like things are disabled:
function test
myStr{1} = 'Allowed';
myStr{2} = str2html ( 'Not Allowed', 'italic', true, 'colour', '#A0A0A0' );
myStr{3} = 'Allowed';
myStr{4} = 'Allowed';
myStr{5} = 'Allowed';
allowed = [1 0 1 1 1]; % flags of items allowed or not.
uicontrol ( 'style', 'listbox', 'position', [0 0 400 200], 'String', myStr, 'Callback', {@checkcb allowed} )
end
function checkcb ( obj, event, allowed )
if allowed(obj.Value) == 0
errordlg ( 'Not allowed' );
obj.Value = find ( allowed, 1, 'first' );
end
end
See example image:
%
  3 件のコメント
Afifa Shaikh
Afifa Shaikh 2020 年 10 月 23 日
Matlab is not able to recognize the keyword 'str2html'
Undefined function or variable 'str2html'.
Andy
Andy 2020 年 10 月 23 日
See file exchange
https://uk.mathworks.com/matlabcentral/fileexchange/46755-str2html?s_tid=srchtitle

サインインしてコメントする。


Amos
Amos 2023 年 4 月 18 日
編集済み: Amos 2023 年 4 月 18 日
As of 2023a, there still isn't (yet?) an enable-state vector, but:
On styling, further to Robert Cummings answer above, and from §Version History: https://au.mathworks.com/help/matlab/ref/matlab.ui.control.dropdown-properties.html
"R2023a: Style drop-down component items
Create styles for drop-down components using the uistyle function, and add the styles to individual items or entire drop-down components using the addStyle function."
On behaviour, (not new, but for completeness) it's then easy, if clumsy, to disregard/reverse selection using ValueChangedFcn and event.PreviousValue (above link: §Callbacks).
When the visual cue doesn't disuade selection, one hopes that user-adaptation follows quickly. The facility to programmatically re-open the droplist could be useful here, but expect item-level enable-state control would be much higher on everyone's wishlists.

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by