Changing color of a string in listbox - MATLAB GUI

조회 수: 3 (최근 30일)
Vincent I
Vincent I 2012년 6월 14일
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

채택된 답변

Walter Roberson
Walter Roberson 2012년 6월 14일
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);
  댓글 수: 1
Walter Roberson
Walter Roberson 2012년 6월 14일
Thanks for catching that; I have edited the correction in.

댓글을 달려면 로그인하십시오.

추가 답변 (6개)

Tom
Tom 2012년 6월 14일

Vincent I
Vincent I 2012년 6월 14일
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
Dr. Seis
Dr. Seis 2012년 6월 14일
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.

댓글을 달려면 로그인하십시오.


Vincent I
Vincent I 2012년 6월 18일
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
Jan
Jan 2012년 6월 18일
@Vincent: You wrote "I have a list of names". Then just add the HTML stuff to one of the names to get it colored.

댓글을 달려면 로그인하십시오.


Vincent I
Vincent I 2012년 6월 18일
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
Vincent I
Vincent I 2012년 6월 18일
one other thing...
How can I change the text color of the colored listbox? thank you
Walter Roberson
Walter Roberson 2012년 6월 18일
<FONT color="cyan">

댓글을 달려면 로그인하십시오.


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

Vincent I
Vincent I 2012년 6월 18일
is there a way to move the new colored text to the top of the listbox?
  댓글 수: 1
Walter Roberson
Walter Roberson 2012년 6월 18일
namestr(validx) = []; %delete it from where it was
namestr = [ {newstr}; namestr ];

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by