# Listbox Selected Row BackgroundColor

**URL:** <https://forum.xojo.com/t/listbox-selected-row-backgroundcolor/70464>\
**Category:** General\
**Created:** [May 29, 2022, 8:28pm UTC](https://forum.xojo.com/t/listbox-selected-row-backgroundcolor/70464 "2022-05-29T20:28:51Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Malumphy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@Chris\_Malumphy](https://forum.xojo.com/u/Chris_Malumphy)\
**Post date:** [May 29, 2022, 8:28pm UTC](https://forum.xojo.com/t/listbox-selected-row-backgroundcolor/70464/1 "2022-05-29T20:28:51Z")

</div>

I have a textfield and button which I use together to search for a particular item in a Listbox. However, I am having difficulty getting the selected Listbox item highlighted consistently. Most of the time, it is highlighted in gray (which isn’t what I want) but sometimes the desired blue coloring is provided. Here is my code.

In the button Action I have

```auto
if TextFieldFindName.text > "" then
  for x as integer = 0 to ListboxResults.LastRowIndex
    if ListboxResults.CellValueAt(x, 0).IndexOf(TextFieldFindName.text) > -1 then
       ListboxResults.Selected(x) = true
       ListboxResults.ScrollPosition = x
      exit
    end if
  next
end if
ListBoxResults.Invalidate 'just experimenting to see if it helps

```

In the Listbox CellBackgroundPaint I have

```auto
dim c as integer
c=column
//alternate row colors
if (row mod 2)=0 then
  g.foreColor = rgb(232,235,255)
  g.fillrect 0,0,g.Width,g.height
end if

//if selected, fill in row with dark blue color and white text 
//otherwise draw with black text 
if me.selected(row) then
  g.foreColor = rgb(66,82,255)
  g.fillrect 0,0,g.Width,g.height
  g.foreColor = rgb(255,255,255)
else
  g.foreColor = rgb(0,0,0)
end if

```

I am currently using Version 2022 Release 1.1 (although the project was started over a decade ago and undergoes frequent updates–although this is a completely new addition to the program). I am running the project on Monterey Version 12.1.

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [May 29, 2022, 8:36pm UTC](https://forum.xojo.com/t/listbox-selected-row-backgroundcolor/70464/2 "2022-05-29T20:36:47Z")

</div>

You need to return True in CellBackgroundPaint to tell the system that you handled the event

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [May 29, 2022, 8:58pm UTC](https://forum.xojo.com/t/listbox-selected-row-backgroundcolor/70464/3 "2022-05-29T20:58:57Z")

</div>

The default selection color is grey when the listbox doesn’t have focus,

---

<div class="post-metadata">

**Author:** ![Chris\_Malumphy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@Chris\_Malumphy](https://forum.xojo.com/u/Chris_Malumphy)\
**Post date:** [May 29, 2022, 9:09pm UTC](https://forum.xojo.com/t/listbox-selected-row-backgroundcolor/70464/4 "2022-05-29T21:09:10Z")

</div>

Thanks Marcus and Tim. Adding return True has improved things. I will test further.

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [May 29, 2022, 11:53pm UTC](https://forum.xojo.com/t/listbox-selected-row-backgroundcolor/70464/5 "2022-05-29T23:53:04Z")

</div>

> [@Chris\_Malumphy](#):
>
> In the Listbox CellBackgroundPaint I have
> 
> ```auto
> dim c as integer
> c=column
> //alternate row colors
> if (row mod 2)=0 then
> g.foreColor = rgb(232,235,255)
> g.fillrect 0,0,g.Width,g.height
> end if
> 
> //if selected, fill in row with dark blue color and white text 
> //otherwise draw with black text 
> if me.selected(row) then
> g.foreColor = rgb(66,82,255)
> g.fillrect 0,0,g.Width,g.height
> g.foreColor = rgb(255,255,255)
> else
> g.foreColor = rgb(0,0,0)
> end if
> 
> ```

That program flow is wrong too. Try in the Listbox CellBackgroundPaint event:

```auto
Function CellBackgroundPaint(g As Graphics, row As Integer, column As Integer) Handles CellBackgroundPaint as Boolean

  //alternate row colors
  If (row Mod 2)=0 Then
    g.foreColor = RGB(232,235,255)
  Else
    g.foreColor = RGB(255,255,255)
  End If
  
  //if selected, fill in row with dark blue color and white text 
  //otherwise draw with black text 
  If Me.selected(row) Then g.foreColor = RGB(66,82,255)
  
  g.fillrect 0,0,g.Width,g.height
  
  Return True
  
End Function

```

Note: if you draw the background in a dark colour then macOS automatically switches the text to white

P.S. If you want to change the text colour (eg to red) you should do that in the CellTextPaint event:

```auto
Function CellTextPaint(g As Graphics, row As Integer, column As Integer, x as Integer, y as Integer) Handles CellTextPaint as Boolean

  If Me.selected(row) Then g.foreColor = color.Red
 
End Function

```

---

<div class="post-metadata">

**Author:** ![Chris\_Malumphy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@Chris\_Malumphy](https://forum.xojo.com/u/Chris_Malumphy)\
**Post date:** [May 30, 2022, 10:58am UTC](https://forum.xojo.com/t/listbox-selected-row-backgroundcolor/70464/6 "2022-05-30T10:58:02Z")

</div>

Markus, your code is cleaner and works except that when using the find button, the Listbox text for the selected item remains black, which is hard to read with a darker background. I am experimenting with better (lighter) backgrounds like rgb(153, 153, 255).

When I make a selection by clicking in the Listbox, the darker background appears AND the text color does turn to white.

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [May 31, 2022, 4:30am UTC](https://forum.xojo.com/t/listbox-selected-row-backgroundcolor/70464/7 "2022-05-31T04:30:31Z")

</div>

Chris, my code works perfectly fine - the problem is whatever code you have.

Here is an example project: [Dropbox - test lb.xojo\_binary\_project.zip - Simplify your life](https://www.dropbox.com/s/ifijxwzpkfcpyd3/test%20lb.xojo_binary_project.zip?dl=0)

---

<div class="post-metadata">

**Author:** ![Chris\_Malumphy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@Chris\_Malumphy](https://forum.xojo.com/u/Chris_Malumphy)\
**Post date:** [May 31, 2022, 5:54pm UTC](https://forum.xojo.com/t/listbox-selected-row-backgroundcolor/70464/8 "2022-05-31T17:54:18Z")

</div>

Thank you for the sample code Markus. As you said it works perfectly, but for whatever reason I also had to add

Me.SetFocus

to the very beginning of my CellBackgroundPaint to get my code make the text color appear in white when I clicked on my find button for the first time.

The help that you and Tim provided is much appreciated. Thanks again.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [May 31, 2022, 6:54pm UTC](https://forum.xojo.com/t/listbox-selected-row-backgroundcolor/70464/9 "2022-05-31T18:54:18Z")

</div>

> [@Chris\_Malumphy](#):
>
> Me.SetFocus

Don’t put that in CellBackgroundPaint. That’s very inefficient. Put listbox SetFocus in your search code just before you set the list index.

---

<div class="post-metadata">

**Author:** ![Chris\_Malumphy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@Chris\_Malumphy](https://forum.xojo.com/u/Chris_Malumphy)\
**Post date:** [May 31, 2022, 7:10pm UTC](https://forum.xojo.com/t/listbox-selected-row-backgroundcolor/70464/10 "2022-05-31T19:10:49Z")

</div>

Will do. Thanks!

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [May 31, 2022, 7:39pm UTC](https://forum.xojo.com/t/listbox-selected-row-backgroundcolor/70464/11 "2022-05-31T19:39:41Z")

</div>

Tim beat me to it, but here is why it is inefficient:

Part of the art of programming is to put your code in the right place.

The code in CellBackgroundPaint is called EVERY TIME a cell scrolls into view, so if you click on the scrollbar and 100 cells come into view you set the focus 100 times.

So every time you scroll around in the listbox your CPU usage goes up. That is the kind of thing that determines whether your app uses 3% or 25% of the CPU or even flickers.

So you need to put the code where and when it is needed - which is when you do the search and set the selected row.
