Listbox Random Color

Hi all,

I am looking for some sample how to change the listbox’s cell background color based from a column tag with random color like apple mail’s inbox.
if there is an email came with different date or sender, its automatically change the color randomly.

link text

is it possible to do it in Xojo’s Listbox ?


in the cellbackgroundpaint event, just change the forecolor to the one you saved in the rowtag of the listbox when you populate the mistbox.

LR + Code Sample (explanation) here:


yes, I have tested cellbackgroundpaint and it works. I am forgot to describe more details of it.

I have data,


I wanted every rows which has the same number, colored automatically with random color,
for example,

there are two rows with text ‘001’, this is has to be in the same background color. and the next one ‘002’ has three rows, should be colored in different color with ‘001’. and so on.

The numbers listed is generated user based. so I cant use rowtag function.


if you have a listbox with let’s say 5 columns with your user data
tells the IDE there is 5 columns, but when you add rows, add a sixth column with these 001-002 datas in it
xojo will display 5 columns, but the six row of datas will be available.
then in the cellbackgroundpaint event, you have access to the data you need, and paint accordingly.

That doesn’t sound very random to me.
How many different numbers can there be?

Here is a list of ‘some websafe colors’
You could pick one of them using something like this method:

TheNumber = val(me.cell(row,1)) mod 140 +1 g.forecolor = myArray(thenumber) g.fillrect .. or whatever

Colors: (put the color value into the array element)… eg myArray(3) = &c00FFFF
and note that color 137 is white!

1,AliceBlue ,&cF0F8FF
2,AntiqueWhite ,&cFAEBD7
3,Aqua ,&c00FFFF
4,Aquamarine ,&c7FFFD4
6, Beige ,&cF5F5DC
7, Bisque,&cFFE4C4
8, Black,&c000000
9, BlanchedAlmond,&cFFEBCD
10, Blue,&c0000FF
11, BlueViolet,&c8A2BE2
12, Brown,&cA52A2A
13, BurlyWood,&cDEB887
14, CadetBlue,&c5F9EA0
15, Chartreuse,&c7FFF00
16, Chocolate,&cD2691E
17, Coral,&cFF7F50
18, CornflowerBlue,&c6495ED
19, Cornsilk,&cFFF8DC
20, Crimson,&cDC143C
21, Cyan,&c00FFFF
22, DarkBlue,&c00008B
23, DarkCyan,&c008B8B
24, DarkGoldenRod,&cB8860B
25, DarkGray,&cA9A9A9
26, DarkGreen,&c006400
27, DarkKhaki,&cBDB76B
28, DarkMagenta,&c8B008B
29, DarkOliveGreen,&c556B2F
30, Darkorange,&cFF8C00
31, DarkOrchid,&c9932CC
32, DarkRed,&c8B0000
33, DarkSalmon,&cE9967A
34, DarkSeaGreen,&c8FBC8F
35, DarkSlateBlue,&c483D8B
36, DarkSlateGray,&c2F4F4F
37, DarkTurquoise,&c00CED1
38, DarkViolet,&c9400D3
39, DeepPink,&cFF1493
40, DeepSkyBlue,&c00BFFF
41, DimGrey,&c696969
42, DodgerBlue,&c1E90FF
43, FireBrick,&cB22222
44, FloralWhite,&cFFFAF0
45, ForestGreen,&c228B22
46, Fuchsia,&cFF00FF
47, Gainsboro,&cDCDCDC
48, GhostWhite,&cF8F8FF
49, Gold,&cFFD700
50, GoldenRod,&cDAA520
51, Grey,&c808080
52, Green,&c008000
53, GreenYellow,&cADFF2F
54, HoneyDew,&cF0FFF0
55, HotPink,&cFF69B4
56, IndianRed ,&cCD5C5C
57, Indigo ,&c4B0082
58, Ivory,&cFFFFF0
59, Khaki,&cF0E68C
60, Lavender,&cE6E6FA
61, LavenderBlush,&cFFF0F5
62, LawnGreen,&c7CFC00
63, LemonChiffon,&cFFFACD
64, LightBlue,&cADD8E6
65, LightCoral,&cF08080
66, LightCyan,&cE0FFFF
67, LightGoldenRodYellow,&cFAFAD2
68, LightGray,&cD3D3D3
69, LightGreen,&c90EE90
70, LightPink,&cFFB6C1
71, LightSalmon,&cFFA07A
72, LightSeaGreen,&c20B2AA
73, LightSkyBlue,&c87CEFA
74, LightSlateGray,&c778899
75, LightSteelBlue,&cB0C4DE
76, LightYellow,&cFFFFE0
77, Lime,&c00FF00
78, LimeGreen,&c32CD32
79, Linen,&cFAF0E6
80, Magenta,&cFF00FF
81, Maroon,&c800000
82, MediumAquaMarine,&c66CDAA
83, MediumBlue,&c0000CD
84, MediumOrchid,&cBA55D3
85, MediumPurple,&c9370D8
86, MediumSeaGreen,&c3CB371
87, MediumSlateBlue,&c7B68EE
88, MediumSpringGreen,&c00FA9A
89, MediumTurquoise,&c48D1CC
90, MediumVioletRed,&cC71585
91, MidnightBlue,&c191970
92, MintCream,&cF5FFFA
93, MistyRose,&cFFE4E1
94, Moccasin,&cFFE4B5
95, NavajoWhite,&cFFDEAD
96, Navy,&c000080
97, OldLace,&cFDF5E6
98, Olive,&c808000
99, OliveDrab,&c6B8E23
100, Orange,&cFFA500
101, OrangeRed,&cFF4500
102, Orchid,&cDA70D6
103, PaleGoldenRod,&cEEE8AA
104, PaleGreen,&c98FB98
105, PaleTurquoise,&cAFEEEE
106, PaleVioletRed,&cD87093
107, PapayaWhip,&cFFEFD5
108, PeachPuff,&cFFDAB9
109, Peru,&cCD853F
110, Pink,&cFFC0CB
111, Plum,&cDDA0DD
112, PowderBlue,&cB0E0E6
113, Purple,&c800080
114, Red,&cFF0000
115, RosyBrown,&cBC8F8F
116, RoyalBlue,&c4169E1
117, SaddleBrown,&c8B4513
118, Salmon,&cFA8072
119, SandyBrown,&cF4A460
120, SeaGreen,&c2E8B57
121, SeaShell,&cFFF5EE
122, Sienna,&cA0522D
123, Silver,&cC0C0C0
124, SkyBlue,&c87CEEB
125, SlateBlue,&c6A5ACD
126, SlateGrey,&c708090
127, Snow,&cFFFAFA
128, SpringGreen,&c00FF7F
129, SteelBlue,&c4682B4
130, Tan,&cD2B48C
131, Teal,&c008080
132, Thistle,&cD8BFD8
133, Tomato,&cFF6347
134, Turquoise,&c40E0D0
135, Violet,&cEE82EE
136, Wheat,&cF5DEB3
137, White,&cFFFFFF
138, WhiteSmoke,&cF5F5F5
139, Yellow,&cFFFF00
140, YellowGreen,&c9ACD32

So you want a random colour, but you want it to be “remembered” if you have another row with the same value?

As you assign the random colours, store them in a dictionary with a key as the value of the row (001 002 etc) and the value of the dictionary as the colour value, then you can quickly look them up for each row and use them if they are already in the dictionary or add a new one if not.


Is 001 in dictionary?
Yes > Use that colour
No > Add a new colour to the dictionary and use that for the current row

I am usure you found your way, so here’s more tuned piece of code:

[code] // To avoid OutOfBoundsException
If Row >= Me.ListCount Then Return False

// Draw The Rows to some Color(s)
If Me.RowTag(Row) = “001” Then
g.ForeColor = RGB(255, 0, 0) // Color for Tag value “001”

ElseIf Me.RowTag(Row) = “002” Then
g.ForeColor = RGB(255, 0, 255) // Color for Tag value “002”
// and so on
End If

// Draw the Row background
g.FillRect(0, 0, g.Width, g.Height)[/code]

Of course, you can use Select Case Me.RowTag(Row, Column) instead of the If Then Else End If block.

The code above is basically what is in the Language Reference (only expanded for more than one Tag value as requested).

When you get a good answer, flag the conversation as answered.

Edit: corrected some typos.

Here’s a sample project:

Listbox Bkgnd Colors .


BTW: I used RowTag (as above) because the OP asked about a Row Tag. Use CellTag if you want to draw on a Cell base and modify the way you color the Cells (instead of the Rows).

The used colors:
You can take Jeff list of colours instead of the cmy() I used. I originally set colours with RGB(), but they were too dark for my taste…

OP said he could not use rowtag

[quote]Is 001 in dictionary?
Yes > Use that colour
No > Add a new colour to the dictionary and use that for the current row
this is exactly what i need. but I don’t know how to coded.

I am making an app for invoicing system, the user sometimes has a problem to do verification based from the physical invoices and its wasting lot of times to do it. most of the items, one to another are the same, the difference is in invoice number which is increasing, because generated number.

I was thinking, if every different invoice number has a color, it will help the user to work faster.


You will find explanations about how to deal with Dictionary here:

Here you go Arief, I hope it helps you on your coding journey :slight_smile:


thanks for your kind help.


Dear Mr.,

c = rgb(r.InRange(0, 255), r.InRange(0, 255), r.InRange(0, 255))

I ever trying to modificate it, but no luck, but this code giving mixed dark & soft color. I can’t read the some of the text of the listbox clearly.

How to define the color manually ? is it possible to have soft color only ? I need around 30 different color.

something like this color.

Color Sample


Raise the bottom range so that the value is r.InRange(160,255). That way, the values will always be on the lighter side.