Listbox Background Color

Hi, I have been searching for a solution to my problem. I have read some of the similar questions from 2016 and they dont work. My question is, can i change the background color of a listbox? Previously they used a canvas or some other code. I tried both and they do not work now. Is there a work around or is Xojo team planning to provide a backgroundColor property for the listbox in the near future? TIA

This is done using the PaintCellBackground event of the ListBox:

Function PaintCellBackground(g As Graphics, row As Integer, column As Integer) Handles PaintCellBackground as Boolean
  g.DrawingColor = &cff0000 '// Color to draw
  g.FillRectangle( 0, 0, g.Width, g.Height )
End Function

Not a great name as it also applies to the area that doesn’t contain cells.

@Anthony_G_Cyphers thank you so much for your quick response. I Have tried that but it does not work. I want the background of the listbox to be a color i select.

Your code works with the cells that have a content but not empty cells. I want the background for empty cells and cells with content to be the same color.

I’m not sure what you’re seeing, but it does work on the area of the ListBox that does not have cells. Are you combining this with some other code that may be overriding the drawing?

Here’s what you should be seeing with just the code I supplied above:
image

Yeah, i do have other code… this is what i have

If row > Me.RowCount - 1 Then Return False

Dim cellValue As String = Me.CellTagAt(row, 0)

g.DrawingColor = &c282828 '// Color to draw
g.FillRectangle( 0, 0, g.Width, g.Height )

If cellValue = "default" Then
  If row = Me.SelectedRowIndex Then
    If Color.IsDarkMode Then
      g.DrawingColor = &c515151
    Else
      g.DrawingColor = &cDDDDDD
    End If
    
    g.FillRoundRectangle(10, 0, g.Width - 20, g.Height, 10, 10)
  End If
  
  Return True
End If

This code only paints the cells that have content but not the empty cells. I replace my code with yours it works but then i lose my feature.

Your first line returns from the event handler function if the drawing area is not part of a cell with content. This is more what you’re looking for:

g.DrawingColor = &c282828 '// Color to draw
g.FillRectangle( 0, 0, g.Width, g.Height )

If row > Me.LastRowIndex Then Return False
Dim cellValue As String = Me.CellTagAt(row, 0)
  
If cellValue = "default" Then
  If row = Me.SelectedRowIndex Then
    If Color.IsDarkMode Then
      g.DrawingColor = &c515151
    Else
      g.DrawingColor = &cDDDDDD
    End If
    
    g.FillRoundRectangle(10, 0, g.Width - 20, g.Height, 10, 10)
  End If
    
  Return True
End If

Thank you, @Anthony_G_Cyphers it worked. I left the line the way it was but i placed the code you provided before it and it worked.

Happy to help.

1 Like