Different color for Listbox items
Afficher commentaires plus anciens
Hey guys, I would like to set different colours of items in a listbox.
The code works for strings like 'a' but it doesn't work for a cell array like this:
data.Properties = {'a';'b';'c';'d';'e'};
I would like to have three cell arrays in a listbox like 'data.Properties'. Each of the cell arrays like 'data.Properties' should have a different colour in this listbox.
Why is a cell array not working and a string is? What do I have to change to get it done with the cell array?
function Test_UiControl_Listbox_small()
%%Test Function
close all
data = createData();
gui = createInterface();
function data = createData()
%%Data from Excel
data.Properties = {'a';'b';'c';'d';'e'};
%Data(1).name = data.Properties; % this is not working but why?
data.Data(1).name = 'a';
data.Data(1).Color = [255 0 0];
data.Data(2).name = 'b';
data.Data(2).Color = [0 255 0];
data.Data(3).name = 'c';
data.Data(3).Color = [0 0 255];
end
function gui = createInterface()
%%Window figure and Panel
gui.Window = figure('Units','normalized','Outerposition',[0 0 1 1],...
'Name','Test ', ...
'NumberTitle', 'off','MenuBar', 'none');
gui.panel_T = uipanel('Units','pixels','Title','Test2',...
'FontSize',10,'Position',[400 200 1100 350]);
pre = '<HTML><FONT color="';
post = '</FONT></HTML>';
listboxStr = cell(numel( data.Data ),1);
for i = 1:numel( data.Data )
str = [pre rgb2Hex( data.Data(i).Color ) '">' data.Data(i).name post];
listboxStr{i} = str;
end
gui.hListBox = uicontrol('parent',gui.panel_T,'Style','list',...
'Position', [20 20 100 100], 'String', listboxStr );
function hexStr = rgb2Hex( rgbColour )
hexStr = reshape( dec2hex( rgbColour, 2 )',1, 6);
end
end
end
Réponses (1)
I recommend to take the time and ask an internet search engine for : "Matlab listbox color". You will get links like:
- https://www.mathworks.com/matlabcentral/answers/153064-change-color-of-each-individual-string-in-a-listbox
- https://undocumentedmatlab.com/blog/listbox-layout-customization
By the way:
switch gui.checkbox{k}
case gui.checkbox{1}
...
This is working but at least confusingly obfuscated. This would be much smarter:
if k == 1
...
1 commentaire
Pi Height
le 4 Août 2018
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!