Xojo2023r3: WebListBox - how to apply background color to the cell?

In Xojo2018 this code in MyList.LineAdded is changing the background color of the cell:

if myMarker = "Y" then
  MyList.CellStyle(lineNum, 0) = ListCellColorAuto
else
  MyList.CellStyle(lineNum, 0) = ListCellManual
end if

What is the equivalent code in Xojo2023r3?

In the DesktopListBox.PaintCellBackground event (or any other event that exposes a g As Graphics object), you can assign the background. For example:

If IsDarkMode Then
    g.DrawingColor = &c22222200
  Else
    g.DrawingColor = &cdddddd00
End If
g.FillRectangle(0,0,g.Width,g.Height)

Edward, I didn’t mention, sorry, this is for web app (not desktop app).

Ah … I see that little detail now in the topic title. :slight_smile:

Tricky but works for me:

Var style As New WebStyle
Style.Value("text-align") = "center"
style.BackgroundColor = &cC0C0C0
style.BorderColor = Color.Black
style.ForegroundColor=Color.Red
style.BorderThickness = 1
style.FontSize=22
Style.Bold = True

Var cellRenderer As New WebListBoxStyleRenderer(style, "text of cell")

Listbox1.CellValueAt(1,0) = cellRenderer

You will figure the rest.

Paweł, to jest dokładnie to czego potrzebowałem, dziękuję bardzo.

Great, thank you very much, assuming cell(1,3) this is exactly what I was looking for

Var style As New WebStyle
Var vText As String

Style.BackgroundColor=Color.Yellow
vText = MyList.CellTextAt(1,3) 

Var cellRenderer As New WebListBoxStyleRenderer(style, vText)

MyList.CellValueAt(1,3) = cellRenderer

Further refinement assuming parameters pRowNo, pColNo, pType - this method will color cell in the web list box.

Var vStyle As New WebStyle
Var vText As String

vText = MyList.CellTextAt(pRowNo,pColNo)

Select Case pType
Case kAlertWarning
  Style.BackgroundColor=Color.Yellow
Case kAlertError
  Style.BackgroundColor=Color.Red
Else
  Return
End Select

Var vCellRenderer As New WebListBoxStyleRenderer(style, vText)

MyList.CellValueAt(pRowNo,pColNo) = vCellRenderer

Style.BackgroundColor=Color.White  // reset default value

I have added the last line to reset background color to keep the web page background color white after refresh.

The interesting part is that the docs and the autocomplete say it should be

MyList.CellTextAt(pRowNo,pColNo) = vCellRenderer

But your code makes more sense.

And a

MyList.CellRendererAt(pRowNo,pColNo) = vCellRenderer

Would make even more.

Yeah, we’ve been talking about CellTextAt and we will probably split them. We will have dedicated methods for setting the text and setting the cell renderer, as it’s confusing. And they won’t return Variants anymore.

But for the moment, CellTextAt is the preferred way to use both, text and cell renderers.

2 Likes