Multi coloured text in a list view item

G

Guest

I have asked this in the windowsforms forum, but thought I would try this one
too just in case. I am developing a game server browser winform application.
The app will query selected servers that are hosting a game (e.g. Quake 3),
and the server will send back a string with various info. One of these pieces
of info will be a
list of players names.

My problem is, players can colour their names using ^<char>, where <char> is
a number between 0 and 9, or a letter. I am currently using a list view to
display the list of players.

Is it at all possible to have multi coloured text in a list view, or similar
control? I have seen it done in other apps (although none that are written
using the .Net framework - for example All Seeing Eye). In a web page this
would be simple - simply wrap each of the letters in the players name that
are different from the previous letters colour in a <font> tag.

Just for the record, I am not interested in the RichTextBox control. This
data needs to be displayed in a grid-like control.

All help much appreciated.

Thanks
Dan
 
M

Morten Wennevik

Hi Dan,

As of Framework 2.0 you will be able to OwnerDraw ListView items, but until then you are out of luck.
 
G

Guest

I suspected as much. As it happens, I have a the Express copy of C# at home.
I appreciate this is not a 2.0 forum, but do you have any info on how this
could be done in 2.0? Or any useful links.

Cheers
Dan
 
M

Morten Wennevik

Well, I haven't tried ownerdrawing ListView items yet, but the principles should be the same as ownerdrawing any other list.

I suppose you will need several calls to Graphics.DrawString, one for each time the color changes and to get the correct position for the next bit of text you will need to use Graphics.MeasureCharacterRanges.

This sample covers the basics of it. You probably don't need to use MeasureItem.
You should note that for ListView you use the DrawListViewItemEventHandler/Args and OwnerDraw needs to be set to true.


I suspected as much. As it happens, I have a the Express copy of C# at home.
I appreciate this is not a 2.0 forum, but do you have any info on how this
could be done in 2.0? Or any useful links.

Cheers
Dan
 
G

Guest

Thanks Morten. I will take a look.

Dan

Morten Wennevik said:
Well, I haven't tried ownerdrawing ListView items yet, but the principles should be the same as ownerdrawing any other list.

I suppose you will need several calls to Graphics.DrawString, one for each time the color changes and to get the correct position for the next bit of text you will need to use Graphics.MeasureCharacterRanges.

This sample covers the basics of it. You probably don't need to use MeasureItem.
You should note that for ListView you use the DrawListViewItemEventHandler/Args and OwnerDraw needs to be set to true.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top