Change Background Colour of Certain Rows in Listbox

I have loaded table data into a listbox and want to change the background colour of the row depending on field value.

As I’m new to Xojo and currently evaluating the software, can someone show me some code to achieve row colour change.

check teh listbox docs for the event CellBackgroundPaint

in cellBackgroundPaint add something similar to the following.

if row mod 2 = 1 then g.foreColor = &cCCCCCC g.fillRect 0, 0, g.width, g.height end if

Now if you want to get the native OS colors for alternating listbox rows… That’s a lot harder.

I am trying to make sense os the tech docs. I have been able to get the alternating row going ok. But I am having trouble changing the row colour depending on a cell value. I have a button that connects to the database and populates the listbox. It also adds a rowtag with the value of “red” on row with curtain values.

I then went into the cellbackgroundpaint event and used the folowing code.

if me.RowTag(row) = “red” Then

   g.ForeColor = RGB(255,0,0)
   g.FillRect(0, 0, g.Width, g.Height)

end if

I’m not sure where I’m going wrong. I keep getting an OutOfBounds Exception Error.

CellbackgroundPaint covers all rows, including rows that don’t have any data. So you must check first.

if row < me.listcount and me.rowTag( row ) = "red" then g.ForeColor = RGB(255,0,0) g.FillRect(0, 0, g.Width, g.Height) end if

All I’ve changed is added a check to make sure that ‘row’ is smaller than the list count.

Thanks, but it only works for the first row.

And return true to indicate you have handled the drawing. Otherwise, the system will draw over what you just did.

if row < me.listcount and me.rowTag( row ) = "red" then
g.ForeColor = RGB(255,0,0)
g.FillRect(0, 0, g.Width, g.Height)
return true
end if

Still only one row. Here is the code on the button that populates the listbox.

  While Not rs.EOF
    
    if rs.Field("product_uom_qty").value >= 100 then
      ListBox1.AddRow(rs.Field("order_no").StringValue, rs.Field("product_uom_qty").Value,      rs.Field("woods_code").Value,rs.Field("readable_text").StringValue)
      ListBox1.RowTag(row) = "red"
      
    Else
      ListBox1.AddRow(rs.Field("order_no").StringValue, rs.Field("product_uom_qty").Value, rs.Field("woods_code").Value)
    End If
    rs.MoveNext
  Wend

Maybe I’m doing something wrong here.

row is an variable declared somewhat earlier in your code as is uninitialized, it is therefor 0:

[quote=78355:@Trevor Campbell]ListBox1.RowTag(row) = “red”
[/quote]
You need to use LastIndex, which returns the index of the last added or inserted row:

ListBox1.RowTag(ListBox1.LastIndex) = "red"

Thanks