MATLAB Answers


Enable / Disable specific listbox item

Asked by Dan
on 13 Oct 2014
Latest activity Commented on by Dan
on 13 Oct 2014
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.


Sign in to comment.


2 Answers

Answer by Image Analyst
on 13 Oct 2014
 Accepted Answer

You can't do that. The workarounds are the two you listed.


Sign in to comment.

Answer by Robert Cumming on 13 Oct 2014

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} )
function checkcb ( obj, event, allowed )
if allowed(obj.Value) == 0
errordlg ( 'Not allowed' );
obj.Value = find ( allowed, 1, 'first' );
See example image:

  1 Comment

Intriguing..... I'll give it a shot! thanks!

Sign in to comment.