rowTag in listbox cellbackgroundpaint

I would like to use cellbackgroundpaint in a listbox to colour the background based on the rowTag.
Having tried this with little success I decided to look on the xojo docs at ListBox.CellBackgroundPaint.
I created a new project, added listbox, a couple of rows and the code from the example.

If Me.CellTag(row, column ) = "Red" Then g.ForeColor = RGB(255, 0, 0) g.FillRect(0, 0, g.Width, g.Height) End If

The code breaks on the first line with an out of bounds exception error.

I am running Xojo 2016r3 on Windows 7 Pro - although have tested this morning on 2016r2 & 2016r1 with same issue.

Any help would be appreciated.

If row < Me.ListCount Then

If Me.CellTag(row, column ) = “Red” Then
g.ForeColor = RGB(255, 0, 0)
g.FillRect(0, 0, g.Width, g.Height)
End If

End If

Hi Richard, many thanks for your quick response. I had tried that and just tried again to confirm.

On further investigation I have found it was crashing at row 4 even though i only had 4 rows (0 to 3).

I have added to your code to check for this and it works fine.

[code]If row > -1 and row < me.listcount Then

If Me.CellTag(row, column ) = “Red” Then
g.ForeColor = RGB(255, 0, 0)
g.FillRect(0, 0, g.Width, g.Height)
End If

End If[/code]

Any ideas why this should be the case?

Because row is zero-based. When rowcount = 4, the last row index is actually 3, as in 0, 1, 2, 3.

CellBackgroundPaint fires on rows even if there is no content in them. This allows you to do things like alternate colored rows, but it does mean that you need to check that there is data before trying to access it.

On the other hand, CellTextPaint only fires on rows which have been added, in this case for rows 0 - 3.

Thanks Greg. Much appreciated.

Pardon the interruption on this thread, but how does one change the text color when you paint the background?
I’d like my text to be white but it is black and the contrast is terrible…

Change the g.forecolor in the celltextPaint event and do not return true

personally I alway just return TRUE in CellTextPaint, and do EVERYTHING in background paint

Also note that for “RED” you are not filling the rectangle, and returning WHITE not RED

[code]Function CellBackgroundPaint(g As Graphics, row As Integer, column As Integer) Handles CellBackgroundPaint as Boolean
dim stat as Boolean = false
if me.LastIndex > -1 and row <= me.LastIndex then

If Me.RowTag(row ) = "Grey" Then
  g.ForeColor = RGB(64, 64, 64)
  g.FillRect(0, 0, g.Width, g.Height)
  stat = true
elseif me.RowTag(row) = "Green" then
  g.ForeColor = RGB(0,64, 0)
  g.FillRect(0,0, g.Width, g.Height)
  stat = true
elseif me.RowTag(row) = "Red" then
  g.ForeColor = RGB(64,0,0)
  g.FillRect(0,0,g.Width, g.Height)
  stat = true
End If

end if

return stat
End Function[/code]

[code]Function CellTextPaint(g As Graphics, row As Integer, column As Integer, x as Integer, y as Integer) Handles CellTextPaint as Boolean
dim stat as Boolean = false
if me.LastIndex > -1 and row <= me.LastIndex then

If Me.RowTag(row ) = "Grey" Then
  g.ForeColor = RGB(255, 255, 255)
  stat = true
elseif me.RowTag(row) = "Green" then
  g.ForeColor = RGB(255, 255, 255)
  stat = true
elseif me.RowTag(row) = "Red" then
  g.ForeColor = RGB(255,255,255)
  stat = true
End If

end if

Return stat

End Function[/code]

Looks bad… I must be doing something wrong.

if you return TRUE that means YOU have dealt with the event… so you probably WON’T see any text

I see what you mean… it just seems wrong to me to say I haven’t handled the event when I actually have ‘partially’ handled the event.
Was I supposed to stroke the text?

try this
CellTextPaint - just return TRUE

cellBackgroundPaint

If row<Me.ListCount Then 
Select Case Me.RowTag(row)
Case "Grey"
g.ForeColor=RGB(64,64,64)
Case "Green"
g.ForeColor=RGB(0,64,0)
Case "Red"
g.ForeColor=RGB(64,0,0)
End Select
g.fillrect 0,0,g.Width,g.height
//
g.ForeColor=Color.white
g.DrawString Me.cell(row,col),5,g.TextAscent
End If

or

dim x as integer = 5
dim y as integer = g.textascent+(g.height-g.textheight)/2
g.drawstring me.cell(row,column),x,y