# exception in cellbackground paint

**URL:** <https://forum.xojo.com/t/exception-in-cellbackground-paint/13098>\
**Category:** General\
**Created:** [December 19, 2013, 8:01pm UTC](https://forum.xojo.com/t/exception-in-cellbackground-paint/13098 "2013-12-19T20:01:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_O\_Brien](https://forum.xojo.com/user_avatar/forum.xojo.com/brian_o_brien/32/20096_2.png) [@Brian\_O\_Brien](https://forum.xojo.com/u/Brian_O_Brien)\
**Post date:** [December 19, 2013, 8:01pm UTC](https://forum.xojo.com/t/exception-in-cellbackground-paint/13098/1 "2013-12-19T20:01:40Z")

</div>

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  
try  
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)  
else  
return false  
end  
g.FillRect(0, 0, g.Width, g.Height)  
return true  
catch OutOfBoundsException  
return false  
end  
end if  
return false

End Function  
[/code]

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

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [December 19, 2013, 8:04pm UTC](https://forum.xojo.com/t/exception-in-cellbackground-paint/13098/2 "2013-12-19T20:04:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jim\_mckay](https://forum.xojo.com/user_avatar/forum.xojo.com/jim_mckay/32/905_2.png) [@jim\_mckay](https://forum.xojo.com/u/jim_mckay)\
**Post date:** [December 19, 2013, 8:05pm UTC](https://forum.xojo.com/t/exception-in-cellbackground-paint/13098/3 "2013-12-19T20:05:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Brian\_O\_Brien](https://forum.xojo.com/user_avatar/forum.xojo.com/brian_o_brien/32/20096_2.png) [@Brian\_O\_Brien](https://forum.xojo.com/u/Brian_O_Brien)\
**Post date:** [December 19, 2013, 9:30pm UTC](https://forum.xojo.com/t/exception-in-cellbackground-paint/13098/4 "2013-12-19T21:30:10Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![Brian\_O\_Brien](https://forum.xojo.com/user_avatar/forum.xojo.com/brian_o_brien/32/20096_2.png) [@Brian\_O\_Brien](https://forum.xojo.com/u/Brian_O_Brien)\
**Post date:** [December 19, 2013, 9:32pm UTC](https://forum.xojo.com/t/exception-in-cellbackground-paint/13098/5 "2013-12-19T21:32:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [December 19, 2013, 9:36pm UTC](https://forum.xojo.com/t/exception-in-cellbackground-paint/13098/6 "2013-12-19T21:36:29Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 4:59pm UTC](https://forum.xojo.com/t/exception-in-cellbackground-paint/13098/7 "2020-10-29T16:59:29Z")

</div>


