# PaintCellBackground issue

**URL:** <https://forum.xojo.com/t/paintcellbackground-issue/76855>\
**Category:** Getting Started\
**Created:** [August 10, 2023, 7:30pm UTC](https://forum.xojo.com/t/paintcellbackground-issue/76855 "2023-08-10T19:30:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Armando\_SORBI](https://forum.xojo.com/user_avatar/forum.xojo.com/armando_sorbi/32/934_2.png) [@Armando\_SORBI](https://forum.xojo.com/u/Armando_SORBI)\
**Post date:** [August 10, 2023, 7:30pm UTC](https://forum.xojo.com/t/paintcellbackground-issue/76855/1 "2023-08-10T19:30:41Z")

</div>

Hello,  
I know it may be a silly issue: I have a 3 column ListBox, Column 1 should be painted yellow anytime it holds the word “Juniores”. Here’s the code:  
"Var jun As Integer  
if row mod 2 = 0 Then  
g.ForeColor = &cf3f6fA  
g.FillRect(0, 0, g.Width, g.Height)  
end if

For i as integer = 0 to Me.LastRowIndex  
if Me.CelltextAt(i, 1) = “Juniores” then  
jun = i  
end If  
If row = jun and Column = 1 Then  
g.DrawingColor = &cFFFF66 //Yellow  
g.FillRectangle(0, 0, g.Width, g.Height)  
End If  
Next"

I knwo it is not the best coding you have seen but it works except that it paints yellow Row 0, column 1 even if it does not meet the criteria.  
I have the same issue in other ListBoxes as well. Only that very first cell (see picture).  
I tried different approaches but to no avail.  
Any body can help?  
Also, if you have a better or nicer coding for the same issue I’d appreciate the suggestion.  
Thank you

 ![Screenshot of Xojo.debug (10-08-2023, 21-23-25)](https://forum.xojo.com/uploads/default/original/2X/e/efc1d93c508e8ce90ed016fa4b51f10274fd36ca.jpeg)

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [August 10, 2023, 7:47pm UTC](https://forum.xojo.com/t/paintcellbackground-issue/76855/2 "2023-08-10T19:47:32Z")

</div>

> [@Armando\_SORBI](#):
>
> Var jun As Integer

Change that to

```auto
Var jun As Integer = -1

```

for example.

Because at first for jun = 0 and Column = 1, so that cell will always be yellow.

Do you have that code in PaintCellBackground? If yes, you don’t need to check all the cells all the time.

Edit: I have seen code like this for PaintCellBackground

```auto
If row > Me.LastRowIndex Then Return True
If Me.CellTextAt(row, 1) = "Juniores" Then
  g.DrawingColor = &cFFFF66 //Yellow
  g.FillRectangle(0, 0, g.Width, g.Height)
End If

```

---

<div class="post-metadata">

**Author:** ![Armando\_SORBI](https://forum.xojo.com/user_avatar/forum.xojo.com/armando_sorbi/32/934_2.png) [@Armando\_SORBI](https://forum.xojo.com/u/Armando_SORBI)\
**Post date:** [August 11, 2023, 12:01pm UTC](https://forum.xojo.com/t/paintcellbackground-issue/76855/3 "2023-08-11T12:01:36Z")

</div>

It works as far as the first cell not being painted but, on the other hand, this code paints the whole row. I’d like to have the single cell painted

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [August 11, 2023, 12:29pm UTC](https://forum.xojo.com/t/paintcellbackground-issue/76855/4 "2023-08-11T12:29:01Z")

</div>

Are you talking about this change in your code?

```auto
Var jun As Integer = -1

```

or using this code?

```auto
If row > Me.LastRowIndex Then Return True
If Me.CellTextAt(row, 1) = "Juniores" Then
  g.DrawingColor = &cFFFF66 //Yellow
  g.FillRectangle(0, 0, g.Width, g.Height)
End If

```

if the latter, then you can add a restriction before the code like:

```auto
if column <> 1 then Return True //do not paint background other than column 1

```

I will test and report back.

Edit: yes the original 5 lines code I posted paint each cell for a row where ‘Juniores’ appear on column 1. Adding the restriction to only paint column 1 makes the code work as you expected.

Adding the final code with comments, that may help you work other cases:

```auto
//do not try to paint rows outside our content
If row > Me.LastRowIndex Then Return True
//do not paint background other than column 1, remove if you want to paint the whole row
If column <> 1 Then Return True
//check (row, column=1) for 'Juniores'
If Me.CellTextAt(row, 1) = "Juniores" Then
  g.DrawingColor = &cFFFF66 //Yellow
  g.FillRectangle(0, 0, g.Width, g.Height) //paint background for the cell
End If

```

---

<div class="post-metadata">

**Author:** ![Armando\_SORBI](https://forum.xojo.com/user_avatar/forum.xojo.com/armando_sorbi/32/934_2.png) [@Armando\_SORBI](https://forum.xojo.com/u/Armando_SORBI)\
**Post date:** [August 11, 2023, 12:41pm UTC](https://forum.xojo.com/t/paintcellbackground-issue/76855/5 "2023-08-11T12:41:10Z")

</div>

It works.  
Thanks a lot

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [August 11, 2023, 12:44pm UTC](https://forum.xojo.com/t/paintcellbackground-issue/76855/6 "2023-08-11T12:44:02Z")

</div>

Happy to help.  
You can check my edit (code with comments) that may help you adapt the code to other needs.
