Conditional formatting in Listbox

Hi, I have a listbox, and would like to have different textcolors depending on the value of a cell… So in column 2 I have numbers .

I want to check, if the number is < 100 then the text (of the number itself) should be grey, it it is > 100 then it should be black.

How can I set this?

I now the cellTextPaint can do this prior to writing, but I’m appending the listbox and filling in the cells, so I have to check and change after… ?

I would like to do something like this:

cellNo = (x,y)
if listbox(cellNo).textvalue < 100 then cell text change to grey it not set to black…

How to code this …


I changed the background of a Row if a Cell (the last Column of the Row in my case) is empty; and it works fine.

I took as example from the docs (ListBox):


This code can be used to do alternate row highlighting:

If row Mod 2 = 0 Then g.ForeColor = &cf3f6fA g.FillRect(0, 0, g.Width, g.Height) End If

This example paints the cell background red if the celltag contains the string “Red”

If Me.CellTag(row, column ) = "Red" Then g.ForeColor = RGB(255, 0, 0) g.FillRect(0, 0, g.Width, g.Height) End If

In CellTextPaint:

if Column = 2 then
   if Val(me.Cell(Row, Column)) < 100 then
      g.ForeColor = &c808080
      g.ForeColor = &c000000

When you add a row to the listbox, the CellTextPaint event is called for each column (assuming the row is visible, of course).

When does the CellTextPaint fire…?

For each cell, or is it possible to turn of visible on listbox, populate and when visible, will it fire only this time ?

Take a look there:

Thanks, one last question…

How do I link/trigg the cellTExtpaint via a checkbox…

i.e. column(1) is tekst, column(2) is checkbox…

Now if I click checkbox to be true, how do I then get the CellTextPaint to fire, or is there and other way to update column1 color?

Hi Tim!

It works very good, but when I redraw or redimension the table, It colors me all the column in Red, what Am I doing wrong?:

if Column = 9 then
  if Val(me.Cell(Row, Column)) <= 0  then
      g.ForeColor = rgb(255,0,0)

      g.ForeColor = &c000000



I resolve it, like the last time that I had issues formatting to currency. Using “CellTAG” instead of Cell