Listbox alternate row coloring

I have a couple of listboxes that show alternate row coloring with

If row Mod 2 = 0 And alternateColorBox.Value Then if (row mod 2)=0 then g.foreColor = rgb(237,243,254) g.fillrect 0,0,g.Width,g.height end if end if

When the window opens it shows the listboxes with this row coloring. There is checkbox that allows the user to switch between row coloring or just plain (white).

When the checkbox is ticked I use:

For row As Integer = 0 To chapterBox.ListCount-1 For col As Integer = 0 To chapterBox.ColumnCount-1 chapterBox.InvalidateCell(row, col) Next Next

A simple .invalidate does not work in Win32. BUT when a Listbox is empty the (re)coloring doesn’t work. How come? Only when there is at least one row. So I use:

[code]
if chapterBox.ListCount = 0 then
chapterBox.AddRow(" ")
For row As Integer = 0 To chapterBox.ListCount-1
For col As Integer = 0 To chapterBox.ColumnCount-1
chapterBox.InvalidateCell(row, col)
Next
Next
chapterBox.DeleteAllRows
else

For row As Integer = 0 To chapterBox.ListCount-1
  For col As Integer = 0 To chapterBox.ColumnCount-1
    chapterBox.InvalidateCell(row, col)
  Next
Next

end if[/code]

How is this possible as the row coloring does work OK when it is opened?

What event handles that first block of code?

CellBackgroundPaint

Have you tried refresh? I’ve seen some unpredictable redrawing on windows (it’s not just a Xojo thing either)

[quote=142383:@Alexander van der Linden]When the checkbox is ticked I use:

For row As Integer = 0 To chapterBox.ListCount-1
  For col As Integer = 0 To chapterBox.ColumnCount-1
    chapterBox.InvalidateCell(row, col)
  Next
Next

A simple .invalidate does not work in Win32. BUT when a Listbox is empty the (re)coloring doesn’t work. How come? Only when there is at least one row. So I use:[/quote]

Maybe you invalidate/recolour only the filled rows? If there are no rows then the For … Next loop isn’t executed.

Why not Listbox.invalidate?

Refresh and invalidate do not work in Windows.

The point with Listbox is that if you use the BackgroundPaint on program start it paints all odd rows even when there is no row data. Just like the Weblistbox.

But if you want to use it again it only functions with a rowcount of 1.

Use InvalidateCell(-1,-1)

Actually it is not a Windows problem. It is the same on OS X.

You can work around it with the following code in the action event of the CheckBox:

LB.AddRow LB.RemoveRow( LB.LastIndex )

Working example project at https://dl.dropboxusercontent.com/u/992591/Xojo/Forum/LB%20row%20colour%20bug.rbp.zip

Tim’s solution is better (it is used in the example project)

There are many issues related to invalidating a single cell in Windows that don’t work as expected. A few Betas back (2014r1.1 I think it was?) related to this issue in Cocoa, and tried to apply similar fixes to the Windows ListBox code. It did make InvalidateCell work better, but broke many other things unexpectedly so they reverted the Windows code back (Cocoa works great, BTW). So in many cases you need to do what Tim suggested, use InvalidateCell(-1,-1) for Win32.

Tim, that works fine.
The solution I use I found in the ‘Good Guy Listbox’ examples from the Xojo Webinars.

Thanks!