Adding Image to row - Error

I have a Listbox and in the CellBackgroundPaint event, I have placed the following code to add a Folder icon if the row contains a Folder or a File icon if the row contains a File.
It loads fine but when I scroll down it throws an error

  if column = 0 then
    if instr(Listbox1.Cell(row,3), "Folder") > 0 then
      Dim Myicon As Picture ' get a reference to the picture you want
      ' draw it flush on the right side of the cell
      g.DrawPicture(folderico, 5, 0) 
      
    elseif instr(Listbox1.Cell(row,3), "Folder") = 0 then
      Dim Myicon As Picture ' get a reference to the picture you want
      ' draw it flush on the right side of the cell
      g.DrawPicture(fileico, 5, 0) 
      
    end if
  end if

If I scroll all the way to the bottom, the program stops and throws an error. Same if I change the directory.

You probably got an outofbounds exception there.

You need:
if column = 0 and row <me.listcount then

CellBackgroundPaint fires for empty rows, ones that aren’t actually there but have space to draw. So Listbox1.Cell(row,3) is out-of-bounds. The fix is a simple test at the very top

If row >= me.ListCount then return
edit: Maximilians’s code is better form

Awesome, thanks a lot!!!