exception in cellbackground paint

I have a timer that adds rows to a listbox.
I added an event handler to listbox1 for cellbackgroundpaint.
That method is throwing an outofbounds exception/

How can that happen?

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

//#pragma BreakOnExceptions false
dim t as string
if me.LastIndex <> -1 then
t = Me.CellTag(row, column) // This is throwing the exception.
select case t
case “RED”
g.ForeColor = RGB(255, 127, 127)
case “YELLOW”
g.ForeColor = RGB(127, 255, 255)
case “BLUE”
g.ForeColor = RGB(127, 127, 255)
case “GREEN”
g.ForeColor = RGB(127, 256, 127)
return false
g.FillRect(0, 0, g.Width, g.Height)
return true
catch OutOfBoundsException
return false
end if
return false

End Function

As you can see I have been trying to catch and handle the exception but when it is thrown my whole application shut down.

Is it an OUTOFBOUNDS exception?

Put this in at the first line

if ROW<0 or ROW>=ME.LISTCOUNT then return false

you are checking LASTINDEX, but using ROW

You need to check that row < listcount. If row is out of bounds then Celltag is nil and I think you’re creating 2 exceptions in the same call… Might be too much for a try/catch?

It is an OUTOFBOUNDS execption.
I am using row because well… it’s in an event handler that should have been generated by the object itself, if I can’t trust these parameters there must be something wrong. (I think.)

P.S. if ROW<0 or ROW>=ME.LISTCOUNT then return false worked!

ROW is valid for CELLPAINT as long as you do not attempt to refer to any attributes of it.
For example if you just want to paint something (like alternating background)
but row and celltags will be NIL if the row index exceeds the populated row count

so basically… the event is called for every VISIBLE row… regardless of its contents. that is why the check I provided :slight_smile: