Alternate row coloring

How to do listbox alternate row coloring? I found this code for listbox alternate row coloring. I’m getting the errors below. I imagine I have to declare some things before. Thanks for the help.

For each row as DatabaseRow in rs
  If row Mod 2 = 0 Then
    g.DrawingColor = &cf3f6fA
    g.FillRectangle(0, 0, g.Width, g.Height)
  End If

CleanShot 2022-08-02 at 13.05.24

Where are you putting this code? It should go in the Listbox paint event. And throw away the For each line.

1 Like

From the LR documentation. I have to declare g as an instance of something. I couldn’t get the graphics section

So the code is outside of the For-loop building the listbox. Will try that.

If it’s from here DesktopListBox — Xojo documentation you need to put it in the PaintCellBackground event and not try to set it when you populate the listbox.

I didn’t ask where you are getting it from; I asked where you are putting it. I told you where it should go and you don’t need to declare g. Your exam question is: why do I not need to declare g.

Uhm, my mind often works in the very literal sense. Will try. Julian S. suggestion to put in the PaintCellBackground event. Wait …

It works! How simple are things when you know how they work … All good. Thanks, and to TimStreater.

1 Like

When I first encountered the ListBox I had the same trouble as you. But by having the code in an event handler, the Framework can compute for us which rows are visible and only call the paint event for those. At least I suppose that’s the raionale.

A good exam question. And I’m failing it.

Hint: look at the PaintCellBackground event handler declaration.