ListBox.CellBackgroundPaint

From Xojo Documentation

Event


ListBox.CellBackgroundPaint(g as Graphics, Row as Integer, Column as Integer) As Boolean

Supported for all project types and targets.

The parameter g is a Graphics object that corresponds to the content area of the cell identified by the parameters Row, Column. 0,0 is the upper left of the cell.

Returns a Boolean. True means the user has handled the background paint and no other processing is to be done with the background. In this case the user is responsible for all highlighting. False means the user wants the default background; this will overwrite your drawing on the row that needs to be highlighted as the row or column will be highlighted as appropriate (according to the platform and the hierarchical style).

Notes

CellBackgroundPaint fires for every visible row in a ListBox, regardless of whether there is an actual row there or not. This enables you to implement the background paint event for the entire ListBox in a consistent way. For example, to do alternating row colors. Because of this, you need to check whether the current row is less than ListCount when accessing the row, for example with the Cell method.

Before CellBackgroundPaint fires, some drawing may have taken place. It isn't safe to assume that the background of the cell is white or that the selection hasn't already been drawn. If you need the background to be clear, you need to clear the background yourself and then return True.

Windows HiDPI

When drawing to graphics on Windows HiDPI with a fractional scale factor you may run into a situation that is referred to as a "pixel crack". The symptom is that your drawing does not fill the entire graphics area causing small gaps between the cells.

In order to avoid this you need to disable anti-aliasing when drawing on Windows in this situation. You can easily do this like so:

// Draw black cell background
If row >= Me.ListCount Then Return False
g.AntiAlias = False
g.DrawingColor = &c000000
g.FillRectangle(0,0, g.Width, g.Height)
Return True

Examples

This code can be used to do alternate row highlighting:

If row Mod 2 = 0 Then
g.DrawingColor = &cf3f6fA
g.FillRectangle(0, 0, g.Width, g.Height)
End If

This example paints the cell background red if the CellTag contains the string "Red":

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