# Listbox Cell Text Color

**URL:** <https://forum.xojo.com/t/listbox-cell-text-color/48280>\
**Category:** General\
**Created:** [April 25, 2019, 2:52am UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280 "2019-04-25T02:52:24Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Speth](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/9dc877/32.png) [@Marc\_Speth](https://forum.xojo.com/u/Marc_Speth)\
**Post date:** [April 25, 2019, 2:52am UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/1 "2019-04-25T02:52:24Z")

</div>

I have read the documentation on CellTextPaint but that is an ‘event’ and doesn’t work (I don’t think) the way I need.

What I’m trying to do is to get the row/column from CellClick to tell me what row the user has clicked on, and then redraw the existing black text within that ListBox row to red. The effect being that when the user clicks on a ListBox row the text in that row changes from the default black to red.

If I were to make up my own command to do this it would say something like “Listbox1.Cell(row,column).TextColor = &cFF0000” and I would place it in the CellClick event for the listbox.

Is there a way to do this?

---

<div class="post-metadata">

**Author:** ![Andrew\_Lambert](https://forum.xojo.com/user_avatar/forum.xojo.com/andrew_lambert/32/215_2.png) [@Andrew\_Lambert](https://forum.xojo.com/u/Andrew_Lambert)\
**Post date:** [April 25, 2019, 3:09am UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/2 "2019-04-25T03:09:59Z")

</div>

> [@433867:@Marc Speth](#):
>
> The effect being that when the user clicks on a ListBox row the text in that row changes from the default black to red.

Use the [ListBox.Selected](http://documentation.xojo.com/api/deprecated/listbox.html#listbox-selected) property in the CellTextPaint event to detect whether the cell being painted belongs to a currently selected row:

`Event CellTextPaint(g As Graphics, row As Integer, column As Integer, x as Integer, y as Integer) As Boolean
  If Me.Selected(row) Then
    // draw the text yourself
    g.ForeColor = &cFF000000
    g.DrawString(Me.Cell(row, column), x, y)
    Return True
  Else
    Return False 
  End If
 End Function`

If you want to do this with a row that isn’t selected (but was clicked on) then you’d need to store the row number from the click event and then retrieve it in the CellTextPaint event.

---

<div class="post-metadata">

**Author:** ![Edwin\_Lau](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/dfb087/32.png) [@Edwin\_Lau](https://forum.xojo.com/u/Edwin_Lau)\
**Post date:** [April 25, 2019, 3:18am UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/3 "2019-04-25T03:18:35Z")

</div>

The Selected works when the row is selected but not specifically the cell in the row that is selected.

---

<div class="post-metadata">

**Author:** ![Andrew\_Lambert](https://forum.xojo.com/user_avatar/forum.xojo.com/andrew_lambert/32/215_2.png) [@Andrew\_Lambert](https://forum.xojo.com/u/Andrew_Lambert)\
**Post date:** [April 25, 2019, 3:32am UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/4 "2019-04-25T03:32:44Z")

</div>

Store the Row and Column parameters from the CellClick event (e.g. as properties of the window) and then compare them to the Row and Column parameters of the CellTextPaint event.

---

<div class="post-metadata">

**Author:** ![KarenA](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/c89c15/32.png) [@KarenA](https://forum.xojo.com/u/KarenA)\
**Post date:** [April 25, 2019, 4:21am UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/5 "2019-04-25T04:21:21Z")

</div>

> [@433871:@Andrew Lambert](#):
>
> Store the Row and Column parameters from the CellClick event (e.g. as properties of the window) and then compare them to the Row and Column parameters of the CellTextPaint event.

I guess I could just try it, but if a row is already selected (because a different cell on that row was already clicked) I am not sure CellTextPaint would fire on all platforms as in normal operation nothing would need to be repainted .

And even if it works now, it might not in the future if more drawing optimization is ever done on the listbox.

To be sure in the cell is aways redrawn in CellClick I would would do:

```auto
If me.Selected(row) Then ' A cell in that row was already clicked on

   If LastRow = row And LastColumn = column Then Return False ' Same as last Cell clicked on

   me.InvalidateCell(row,column) ' Make sure selected cell always gets redrawn
   me.InvalidateCell(LastRow, LastColumn) ' Make sure the prior cell selected always gets redrawn

End if

LastRow = row
LastColumn = column
```

Also In CellTextPaint I would let the framework draw the text and just set the color:

```auto
If row = LastRow And column = LastColumn And Me.Selected(row) Then
    g.ForeColor = &cFF000000
End If
```

---

<div class="post-metadata">

**Author:** ![Marc\_Speth](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/9dc877/32.png) [@Marc\_Speth](https://forum.xojo.com/u/Marc_Speth)\
**Post date:** [April 26, 2019, 1:22am UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/6 "2019-04-26T01:22:44Z")

</div>

Andrew’s method works very nicely … It colors the row that was clicked perfectly. But when you click another row the previously clicked row reverts back to black text.

I forgot to give one more parameter.

I would like for the rows that have been clicked to remain red to tell the user which rows have been clicked on so far.

Is there a way to do that?

---

<div class="post-metadata">

**Author:** ![KarenA](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/c89c15/32.png) [@KarenA](https://forum.xojo.com/u/KarenA)\
**Post date:** [April 26, 2019, 1:58am UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/7 "2019-04-26T01:58:22Z")

</div>

[quote=434018:@Marc Speth]Andrew’s method works very nicely … It colors the row that was clicked perfectly. But when you click another row the previously clicked row reverts back to black text.

I forgot to give one more parameter.

I would like for the rows that have been clicked to remain red to tell the user which rows have been clicked on so far.

Is there a way to do that?[/quote]

Retain a list of previously clicked rows either in an array or a dictionary.

---

<div class="post-metadata">

**Author:** ![Marc\_Speth](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/9dc877/32.png) [@Marc\_Speth](https://forum.xojo.com/u/Marc_Speth)\
**Post date:** [April 26, 2019, 7:53pm UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/8 "2019-04-26T19:53:49Z")

</div>

… and implement that list how? … in what event?

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [April 26, 2019, 8:07pm UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/9 "2019-04-26T20:07:02Z")

</div>

> [@433867:@Marc Speth](#):
>
> CellClick

---

<div class="post-metadata">

**Author:** ![KarenA](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/c89c15/32.png) [@KarenA](https://forum.xojo.com/u/KarenA)\
**Post date:** [April 26, 2019, 10:10pm UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/10 "2019-04-26T22:10:37Z")

</div>

> [@434137:@Marc Speth](#):
>
> … and implement that list how? … in what event?

Sorry if this sound harsh but it’s IMO it’s true…  
Given everything in this the thread, you should be able to figure that out yourself. Giving you more specifics at this point will hurt you more than help you in the long run.

- karen

---

<div class="post-metadata">

**Author:** ![Marc\_Speth](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/9dc877/32.png) [@Marc\_Speth](https://forum.xojo.com/u/Marc_Speth)\
**Post date:** [April 26, 2019, 11:41pm UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/11 "2019-04-26T23:41:27Z")

</div>

I have tried to change the cell text color for a range of cells inside CellClick event. But where the CellTextPaint event passes g, the graphics object, to you, as well as Row, Column, X, and Y, the CellClick event doesn’t. Therefore I have to supply these myself.

Row and Column are easy. X and Y I can guess from the experiments I did in the CellTextPaint event. But so far I have not been successful at getting the graphics object for the ListBox, or perhaps the Cell.

Even if I put Dim ‘g As Graphics’ at the beginning of the CellClick event, when it tries to execute the:  
g.ForeColor = &cFF000000  
g.DrawString(Me.Cell(row, column), x, y)  
… commands I get the Nil Object error because I don’t know how to get the graphics property of the ListBox, or Cell, or perhaps Row to be passed to those command lines.

---

<div class="post-metadata">

**Author:** ![Marc\_Speth](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/9dc877/32.png) [@Marc\_Speth](https://forum.xojo.com/u/Marc_Speth)\
**Post date:** [April 26, 2019, 11:51pm UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/12 "2019-04-26T23:51:50Z")

</div>

I have also tried forcing a Select for a row in software, thereby forcing a redraw of that row, thereby triggering a text recolor for that row in CellTextPaint. But so far it appears that Select is can only be ‘passed’ and not ‘forced’.

---

<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:** [April 26, 2019, 11:59pm UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/13 "2019-04-26T23:59:23Z")

</div>

You need to treat the events as completely separate. In CellClick, you record the row/column and then invalidate the listbox. That causes the CellTextPaint events to fire. In CellTextPaint, you check each cell against your list of cells and set the color accordingly. There are many ways to store the row/column of the clicks. Tell us what you have tried, and we can guide you from there.

---

<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:** [April 27, 2019, 12:00am UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/14 "2019-04-27T00:00:53Z")

</div>

In cellClick you can add the row to a list property in the window ClickedRows(-1) as integer then call invalidateCell(row,column) to cause it to repaint.

In the celltextPaint, check the list for the row.  
If ClickedRows.indexof(Row) \> -1 then g.forecolor=&cff0000

You can’t access the Graphics property of a control, except as it is passed to you in an event.

---

<div class="post-metadata">

**Author:** ![Marc\_Speth](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/9dc877/32.png) [@Marc\_Speth](https://forum.xojo.com/u/Marc_Speth)\
**Post date:** [April 27, 2019, 3:25am UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/15 "2019-04-27T03:25:20Z")

</div>

Thank you Tim and Jim.

That was what I needed to know to get it working as desired.

Thank you!!!  
Thank you!!!  
Thank you!!!

I didn’t know before that “InvalidateCell” would force a repaint.

In fact, before this discussion I didn’t know that InvalidateCell even existed.

---

<div class="post-metadata">

**Author:** ![Marc\_Speth](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/9dc877/32.png) [@Marc\_Speth](https://forum.xojo.com/u/Marc_Speth)\
**Post date:** [April 27, 2019, 3:36am UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/16 "2019-04-27T03:36:57Z")

</div>

I first started programming in 1978 in the days of Fortran and BASIC linear programming. So wrapping my head around the concept Objects, Inheritance, and Events has proven to be a significant challenge for me.

I come from the days when if you wanted something to be displayed on the screen you typed '10 PRINT “This is what I want on the screen.” And if you wanted to erase it you used VTAB and HTAB to get back to that position on the screen and then issued a '30 PRINT " " ’ to print blank spaces over it. There was no ‘canvas’ and graphics properties that you had to DRAW into.

So this has been an uphill battle for me to retrain my brain.

And again, I thank you for your help.

---

<div class="post-metadata">

**Author:** ![Marc\_Speth](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/9dc877/32.png) [@Marc\_Speth](https://forum.xojo.com/u/Marc_Speth)\
**Post date:** [April 27, 2019, 3:42am UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/17 "2019-04-27T03:42:07Z")

</div>

Darn! I wanted to give both Tim AND Jim credit for solving my problem because it was the combined information from both posts that led me to the solution.

But the system will only let me click on one “this solved my problem”.

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [April 27, 2019, 9:39pm UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/18 "2019-04-27T21:39:21Z")

</div>

> [@434175:@Marc Speth](#):
>
> In fact, before this discussion I didn’t know that InvalidateCell even existed.

Go to the ListBox documentation and read the docs about each event, property, and method. I had to do that a few times but eventually it stuck (or some of it did, anyway).

---

<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, 7:37pm UTC](https://forum.xojo.com/t/listbox-cell-text-color/48280/19 "2020-10-29T19:37:57Z")

</div>


