# Dynamic listbox cell colour?

Hi,
If I have a 3 column listbox, with the following values:

Column 0 is empty.
Column 1 contains 205,55,0 (rgb colour code for a shade of red).
Column 2 contains CD3700 (hex colour code, for the same shade of red).

Is it possible to make the cell of column 0 of each listbox row have the background colour which is displayed in column 1 (or column 2, as they will be the same colour).

I do not know which colour will be stored in column 1 and 2 of each row, so I can’t hard code it - I need to somehow do it dynamically.

Hope my question made sense - I basically want column 0 of each listbox row to be the same colour as in column 1 or 2 of that particular row.

Examples:
If row 1 columns 1 and 2 have the colour code for blue - make column 0 of that row blue.
If row 2 columns 1 and 2 have the colour code for red - make column 0 of that row red.

Thank you all in advance.

I’m not much of an expert and I expect others will come up with other solutions, but…

In the cellbackgroundpaint event of the listbox

``````if column = 0 Then
g.forecolor = <read value from me.cell(row, 1)>
g.fillrect(0, 0, g.width, g.height)
End If

Return True``````

Hope this helps.

Thanks Keir - but that did not work at all.
Line 2 is not valid Xojo code

Thank you for trying to help

Given the hex values for a color, you can use a variant to convert it.

``````dim v as variant
dim c as color
dim s as string = "CD3700"
v = "&c" + s
c = v``````

Thanks Tim, but what I am struggling with is how to make the background colour of column 0, the same as the colour code which is in each corresponding column 1?

Examples:

Row 1:
Column 1 has the value 000000 - so make the background colour of column 0 of that row, black.

Row 2:
Column 1 has the value FFFFFF - so make the background colour of column 0 of that row, white.

Hope that made more sense.

That’s what Kier’s code does. Combine the 2 snippets.

Tim,
I really can’t see how to combine the 2 snippets

I know that may sound silly to you, but I am still learning (or trying to), and it’s driving me nuts.

The best I can come up with is:

[code]dim v as variant
dim c as color
dim s as string = me.cell(row, 1)
v = “&c” + s
c = v

if column = 0 Then
g.forecolor = c
g.fillrect(0, 0, g.width, g.height)
End If

Return True[/code]

That code seems to work ok. You should move the `return true` inside the `if` statement so you don’t kill highlighting, but otherwise it looks correct. Assuming of course, you moved the hex code from column 2 to column 1.

Thanks Tim,
I couldn’t actually test it because I am on my iPad at the moment.

I will try it later.

Thank you!

Then knock off this silliness. You’re much better at this than you’re giving yourself credit for.

I did manage to combine them - that makes a change

Tim,
just a quick question.

Why do I need the line: dim c as colour?
Would the code below not achieve the same result?

[code]dim v as variant
dim s as string = me.cell(row, 1)
v = “&c” + s

if column = 0 Then
g.forecolor = v
g.fillrect(0, 0, g.width, g.height)
End If[/code]

That’s correct. As part of combining them, you can drop `c as color`.

You could also drop `s as string`, too.

``````dim v as variant

if column = 0 then
v = "&c" + me.cell(row, 1)
g.forecolor = v
g.fillrect(0, 0, g.width, g.height)
return true
End If``````

Thank you!

And, if you want to make Norman happy, you can move the first three lines, the two dims and the calculation of v, following the if statement. After all, if you are not processing column 0 then you don’t need the dims and the calculation of v.

Hi,
When I have this code inside the CellBackgroundPaint event - I get an out of bounds error?

Could this be due to that when first run, my database is empty, and therefore, there is no text in column1???

[code] // SET ALTERNATING ROW COLOURS
if row mod 2 <> 0 then
g.ForeColor=RGB(255,255,255)
else
g.ForeColor=RGB(240,246,255)
end if

g.FillRect 0,0,g.width,g.height

// SET THE SELECTED ROW COLOUR
If Me.Selected(row) Then
g.FillRect (0, 0, g.Width, g.Height)
Return True
End If

dim v as variant

if column = 0 then
v = “&c” + me.cell(row, 1)
g.forecolor = v
g.fillrect(0, 0, g.width, g.height)
return true
End If[/code]

Thanks.

CellBackgroundPaint fires for every visible cell, whether they have data in them or not. ListBox.Cell() only works for cells that have data in them - otherwise, you get an OutOfBounds exception.

You need to test for `row < me.ListCount` before you try to access `me.Cell(row, 1)`

Tim,
Thanks - I already changed the code to that below and it now seems to work perfectly.

Seems I am actually starting to comprehend this developing thing

[code] // COLOURISE THE FIRST LISTBOX COLUMN (CORRESPONDING TO THE HEX COLUMN VALUE)
dim v as variant
Dim i As Integer= Listbox2.ListCount

if column = 0 and i >= 1 then
v = “&c” + me.cell(row, 1)
g.forecolor = v
g.fillrect(0, 0, g.width, g.height)
return true
End If[/code]