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)
    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 !)