Backcolor for a ListBox Cell

Hi.
I need to change color in a cellbox of ListBox.
I tested: ListBox1.CellBold(4, 2) = True and is Ok.

But, i need if exist similar comand to: ListBox1.CellBackColor(4, 2) = &cf3f6fA ???

This may help:
https://documentation.xojo.com/api/user_interface/desktop/desktoplistbox.html#desktoplistbox-paintcellbackground

I don’t think there is a simple way like what you wrote.

Correct, you can’t change a cell’s background directly. It can only be done from within the PaintCellBackground event (or CellBackgroundPaint for the older desktop listbox). You can use the example they give for using the CellTag, or use your own variable/array to indicate whether to “paint” the cell and with what color.

Make sure you understand that all the “paint” events fire on their own, whenever their content needs to be redrawn (scroll, resize, another window passes over, etc.). So you will want to call Refresh or RefreshCell after setting the color if you’re doing it “on the fly” after the listbox is already displayed.

in PaintCellBackgrounf i have inserti this code

If Me.CellTextAt(row, column ) = "25" Then
  g.DrawingColor = RGB(255, 0, 0)
  g.FillRectangle(0, 0, g.Width, g.Height)
End If

But in the first row return an error, why ?

and more:

e come faccio a creare una condizione , ad esempio dentro un ciclo , per colorare una cella ?
Ex: For X=0 to 10
For Y=0 to 10
if cell(X,Y)=40 then
cell(X,Y).cellbackcolor=color.red

i also tried: PaintCelltext

if me.CellTextAt(row,column)=“25” then
messagebox (“CIAO”)
end if
but instead of 25, how do I pass a value?

when you say this its important to also say WHAT error as that will go a long way to determine the answer

the listbox will try to draw rows & columns that have no data in them.
And my suspicion is that is what’s going on and what error you get

I managed some command like this one you want, with a listbox subclass
you add properties to store each cell properties. I added textcolor and textbackgroundcolor in a structure (you can use a class) . I used an array to store row,column, textcolor and backgroundcolor of each cell that have a different color than standard.
then in the cellbackgroundpaint event I draw the cell according to these properties.
this is for API1 but should be easily coded for API2

Subclassing is one of the most powerful things in Xojo and very few people seem to do it at all

Exactly. You need to add a check that the current row is valid:

If row <= Me.LastRowIndex And Me.CellTextAt(row, column ) = "25" Then
  g.DrawingColor = RGB(255, 0, 0)
  g.FillRectangle(0, 0, g.Width, g.Height)
End If

Why do you say that? I’m not a pro and I do it whenever it makes sense.

-Karen

You are far more experienced that you might guess :slight_smile:

Lots of project people will just drop listbox on the layout and then hack & slash their way through the events to try & do something where a subclass would make it simpler AND more reusable

:man_shrugging: