Listbox images

Unlike CellTextPaint, CellBackgroundPaint is called for every visible row, whether it has data in it or not. So you have to test whether Row is in bounds.

Function CellBackgroundPaint(g As Graphics, row As Integer, column As Integer) Handles CellBackgroundPaint as Boolean
  
  
  if column = 0 and row < me.ListCount  then
    dim unit as UnitClass
    unit  =  (me.CellTag(row,column)) // <----- Crash
    if unit <> nil then
      g.drawpicture (unit.Image,0,0)
    end if
  end if
  
  // 
  
End Function

[quote=442179:@Tim Hare]Unlike CellTextPaint, CellBackgroundPaint is called for every visible row, whether it has data in it or not. So you have to test whether Row is in bounds.

[code]
Function CellBackgroundPaint(g As Graphics, row As Integer, column As Integer) Handles CellBackgroundPaint as Boolean

if column = 0 and row < me.ListCount then
dim unit as UnitClass
unit = (me.CellTag(row,column)) // <----- Crash
if unit <> nil then
g.drawpicture (unit.Image,0,0)
end if
end if

//

End Function
[/code][/quote]
Can you be more explicit?, I see that CellBackgroundPaint is called even when the list is empty, if I use
if me.listcount > 0 then…
to avoid trigger the function when the list is empty, I get the crash upon adding any row.

Thanks to all, finally it works, Tim gave me the clue.

[code]Function CellBackgroundPaint(g As Graphics, row As Integer, column As Integer) Handles CellBackgroundPaint as Boolean

if me.ListCount > row then
if column = 0 then

  dim unit as UnitClass
  unit  =  me.CellTag(row,column)
  if unit <> nil then
    g.drawpicture (unit.Image,0,0)
  end if
end if

end if
//

End Function
[/code]