# ListBox 3 alternate background colors

What is wrong in the code below (look at the result screen shot):

``````
Function CellBackgroundPaint(g As Graphics, row As Integer, column As Integer) Handles CellBackgroundPaint as Boolean
If row Mod 2 = 0 Then
g.ForeColor = cmy(0.20,0.00,0.00)
ElseIf row Mod 3 = 0 Then
g.ForeColor = cmy(0.00,0.20,0.00)
Else
g.ForeColor = cmy(0.00,0.00,0.20)
End If

// Draw the Row Background
g.FillRect(0, 0, g.Width, g.Height)
End Function
``````

I was awaiting a pink background every 3 Rows, and I get one at Row = 2, then at every 6 other Rowâ€¦

I donâ€™t think you can use the â€śmodâ€ť operator to handle this situation. The â€śdivisibilityâ€ť of numbers is not sequential. I could use an external variable, at window level or ContainerControl, to â€śmoveâ€ť from time to time. For example 1 = cyan, 2 = magenta, 3 = yellow and, for each â€śFillRectâ€ť, increments of 1 (if the value is 3, reset it to one). A little dirty maybe, but it should work

As said by Ant, this will not work because of arithmetic:

e.g. 6 is a multiple of 2 as well as a multiple of 3, and because the test for
2 is earlier than that for 3 it will have priority.

The easiest to resolve this is to use a â€śCASEâ€ť - structure.

``````Select case row mod 3
case 0
g.ForeColor = cmy(0.20,0.00,0.00)
case 1
g.ForeColor = cmy(0.00,0.00,0.20)
case 2
g.ForeColor = cmy(0.00,0.20,0.00)
end select
``````

?

1 Like

We have a winner her: Thank you Jeffâ€¦ works as I wanted.

OK. After reading the LR, reading once more this thread and Jeff answer, I now understand: I was wrong (it was too easy !)