Multiple font colors in Listbox cell

Hello all,

I’m using the .CellTextPaint event to change the font color of the cells in my listbox (keeping track of the colors with .CellTag).

But I’d like to have multiple font colors within one cell. I am displaying an HTML hex code and I want the R, G, and B portions colored appropriately.

So the first character should be black (#), second and third should be red, fourth and fifth should be green, and sixth and seventh should be blue.

As an example, let’s say the cell contains the string “#FFCC11

If I were writing what I want it to look like in HTML, it would be:

 <font color=black>#</font><font color=red>FF</font><font color=green>CC</font><font color=blue>11</font>

How can I accomplish this in a Listbox cell?

As you are using a paint event and its graphics object, it works just like doing it in any other graphics object (like the one a canvas has).

So you need much more code and a bit of effort, eg determine the width of each part that you want to have, set the colour for the first one, draw it with DrawString, set the colour for the second, draw it at the position where it needs to be, etc

You could impress everyone by coming up with a general method that takes a StyledText and draws it :wink:


[code]Dim rr,rg, rb As Color
Dim myC, myR, myG, myB, myColor As String

myColor = Me.List(row)

myC = myColor.Mid(1, 1) // #
myR = myColor.Mid(2, 2) // R
myG = myColor.Mid(4, 2) // G
myB = myColor.Mid(6, 2) // B

g.Bold = True
g.ForeColor = &c00000000
g.DrawString myC, x, y

rr = &cC6001A00
g.forecolor = rr
g.DrawString myR, g.StringWidth(myC), y

rg = &c12610200
g.forecolor = rg
g.DrawString myG, g.StringWidth(myC) + g.StringWidth(myR), y

rb = &c0000FE00
g.forecolor = rb
g.DrawString myB, g.StringWidth(myC) + g.StringWidth(myR) + g.StringWidth(myG), y

Return True[/code]

WOW! Thank you both for the info and the code.