Random Listbox's row Backcolor


I am looking for some sample to paint listbox’s row backolor, but using random color method.

so, if the criteria of rows is not match, the the backolor should generate color randomly.

is it possible to do with xojo listbox ?

any helps.?


I am not sure your user will understand.

Now, to paint the ListBox area without Row (if there are empty rooms), you simply test the number of Max Rows like

If Row > RowCount Then
// Paint your Listbox background without Row here
End If

There is a RANDOM command in Xojo.

Actually what I need is something like this,

dim cd as string

Select case cd

case "1"
if column=0 then g.foreColor=rgb(255, 0, 0)
if column=1 then g.foreColor=rgb(255, 0, 0)
if column=2 then g.foreColor=rgb(255, 0, 0)

case "2"
if column=0 then g.foreColor=rgb(26, 32, 232)
if column=1 then g.foreColor=rgb(26, 32, 232)
if column=2 then g.foreColor=rgb(26, 32, 232)

case "3"
if column=0 then g.foreColor=rgb(163, 3, 158)
if column=1 then g.foreColor=rgb(163, 3, 158)
if column=2 then g.foreColor=rgb(163, 3, 158)

case "4"
if column=0 then g.foreColor=rgb(145, 145, 0)
if column=1 then g.foreColor=rgb(145, 145, 0)
if column=2 then g.foreColor=rgb(145, 145, 0)

case "5"
if column=0 then g.foreColor=rgb(0, 148, 148)
if column=1 then g.foreColor=rgb(0, 148, 148)
if column=2 then g.foreColor=rgb(0, 148, 148)

case "6"
if column=0 then g.foreColor=rgb(10, 173, 24)
if column=1 then g.foreColor=rgb(10, 173, 24)
if column=2 then g.foreColor=rgb(10, 173, 24)

case "7"
if column=0 then g.foreColor=rgb(68, 62, 77)
if column=1 then g.foreColor=rgb(68, 62, 77)
if column=2 then g.foreColor=rgb(68, 62, 77)

case "8"
if column=0 then g.foreColor=rgb(158, 58, 0)
if column=1 then g.foreColor=rgb(158, 58, 0)
if column=2 then g.foreColor=rgb(158, 58, 0)

case "9"
if column=0 then g.foreColor=rgb(1, 86, 110)
if column=1 then g.foreColor=rgb(1, 86, 110)
if column=2 then g.foreColor=rgb(1, 86, 110)

end select

but I need the backcolor/forecolor is paint automatically by using random color without defining the color manually.
I need every different row criteria automatically paint using random color method.


experiment with

g.foreColor = rgb(rnd()*256,rnd()*256,rnd()*256)

and as a alternative

var c as Color
c = RGB(rnd()*256,rnd()*256,rnd()*256)
g.foreColor = c

1 Like

hi, yes this works.
but, how to keep the same color on rows that have same criteria

all rows number 0006 or 0007 should have the same color.


I’m having a difficult time understanding what you want.

Do you want “different” colors which you define, or “random” colors which the computer picks? How many different colors do you need?

When your selection criteria is met, should it be applied to all the columns in that row or just certain columns in the row?

Here is a sample of computer generated random colors (which look lousy; it would be better if you selected the colors yourself). Each column in a row is the same color, and some rows (like the purple rows 6 and 7, zero based) have the same color according to my instructions:

I created a global Colors(10) array property for the colors

then had the computer randomly define the colors in the window’s Event Handlers Open event

var i as integer
for i = 0 to 10
  colors(i) = rgb(rnd()*256, rnd()*256, rnd()*256)

and applied the appropriate color in the Listbox CellBackgroundPaint according to the specifications I laid out via the Select Case command

select case row 
case 0 
  g.foreColor = colors(0)
case 1 
  g.foreColor = colors(1)
case 6, 7
  g.foreColor = colors(6)
  g.foreColor = colors(row mod 10)

g.fillrect 0,0,g.Width,g.Height
Return True
1 Like

You could use the Random class and set the Seed property based on the content somehow. Then similar rows would get the same random color.

1 Like

Create a constant and store in there the color for those rows that meet the criteria you need, set the color of such row to the constant value.

So you dont want random colors… :wink:

Create a Dictionary
If the criteria is ‘the row has text of 007’, then we need to map 007 to a color.
using a dictionary, you could do something like this:
(you could use an array instead)

myDc.value("1") = rgb(255, 0, 0)
myDc.value("2") = rgb(26, 32, 232)
myDc.value("3") = rgb(0,0,255)

Then in the paint event:

g.foreColor = myDc.value(me.list(row)) 
//If the text is actually "002" then change the strings in the dictionary

1 Like


This is almost the same like what I did.
I just realize, the thing that I am trying to do will not work with random color, it has to be defined manually.

Thanks for all the helps.


The Dolphins in the playoffs. Good times…

It was a while ago for the Dolphins. Tied for the third current longest playoff drought. But I can’t boast much about my team. I’m a Browns fan.

It’s going to be interesting to see who will make some noise first. The Browns had so much potential just a few years ago. I wanted Mayfield on the Dolphins so bad. There’s a reason I’m not a GM. Lol.

Sorry for the off topic…

1 Like