Changing color of a string in listbox - MATLAB GUI

I have a list of names in a listbox. What I would like to do is have a certain selected name change color, from red to green, when I click a button.
Thank you

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 14 Jun. 2012
Don't add the name directly: add the HTML-ized version of the name.
htmlname = sprintf('<HTML><BODY bgcolor="%s">%s', 'red', ThisEntryName);
And in the callback:
namestr = cellstr(get(hObject, 'String'));
validx = get(hObject, 'Value');
newstr = regexprep(namestr{validx}, '"red"','"green"');
namestr{validx} = newstr;
set(hObject, 'String', namestr);

Weitere Antworten (6)

Vincent I
Vincent I am 14 Jun. 2012

0 Stimmen

I have already looked at this article and unfortunately it doesnt work for what i want to do...
in the listbox with the names i can either add or delete the names.
the html method would not work for me
any other ideas? thank you

1 Kommentar

Dr. Seis
Dr. Seis am 14 Jun. 2012
What you are generally describing above should work with HTML... can you post a few pictures of what you envision? Or can you post the cell-string you are using to apply to the listbox... there might be a formatting issue that is preventing it from working.

Melden Sie sich an, um zu kommentieren.

Vincent I
Vincent I am 18 Jun. 2012

0 Stimmen

Walter, i'm not sure what do you mean or what to do with this line or where to add it. By name do you mean string name or listbox name?
Don't add the name directly: add the HTML-ized version of the name.
htmlname = sprintf('<HTML><BODY bgcolor="%s">%s', 'red', ThisEntryName);

1 Kommentar

Jan
Jan am 18 Jun. 2012
@Vincent: You wrote "I have a list of names". Then just add the HTML stuff to one of the names to get it colored.

Melden Sie sich an, um zu kommentieren.

Vincent I
Vincent I am 18 Jun. 2012

0 Stimmen

What i ended up doing is this:
contents = cellstr(get(handles.Names_Listbox,'String'));
NewText = contents{get(handles.Names_Listbox,'Value')};
NewColor = sprintf('<HTML><BODY bgcolor="%s">%s', 'green', NewText);
set(handles.Names_Listbox,'Value',[])
set(handles.Names_Listbox, 'String', cat(1, get(handles.Names_Listbox,'String'), NewColor))
it works great THANK YOU. however, is there a way to move the new colored text to either the top of the list or to stay in the same place that it was initially?
Walter, I tried to follow your exemple in the callback but for some reason didnt do anything for me.
Thank you

5 Kommentare

The code I gave puts the entry back in the same place it was.
Record the Value before you clear it. assign contents{} at that value to be the new line. set() the String to be the changed "contents" cell array.
Vincent I
Vincent I am 18 Jun. 2012
ok GOT IT
contents = cellstr(get(handles.Names_Listbox,'String'));
NewText = contents{get(handles.Names_Listbox,'Value')};
NewColor = sprintf('<HTML><BODY bgcolor="%s">%s', 'green', NewText);
namestr = cellstr(get(handles.Names_Listbox, 'String'));
validx = get(handles.Names_Listbox, 'Value');
newstr = regexprep(NewColor, '"red"','"blue"');
namestr{validx} = newstr;
set(handles.Names_Listbox, 'String', namestr);
its posible that few lines might not be needed, i have to do some tweaking, but as of right now it works. thank you very much
Vincent I
Vincent I am 18 Jun. 2012
one other thing...
How can I change the text color of the colored listbox? thank you
<FONT color="cyan">

Melden Sie sich an, um zu kommentieren.

Vincent I
Vincent I am 18 Jun. 2012

0 Stimmen

NVM
NewColor = sprintf('<HTML><BODY bgcolor="green" text="white">%s', NewText);
THANK YOU VERY MUCH for all your help...
Vincent I
Vincent I am 18 Jun. 2012

0 Stimmen

is there a way to move the new colored text to the top of the listbox?

1 Kommentar

namestr(validx) = []; %delete it from where it was
namestr = [ {newstr}; namestr ];

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by