Listbox row colors

I want to change the colour of certain rows of a listbox, depending on the text being added. How do I do that?

in the CellBackgroundPaint event on the ListBox :

  If (row mod 2) = 0 Then
    g.ForeColor = RGB(232,235,255) ' or a hexa color like &cE8EBFF
    g.FillRect 0, 0, g.Width, g.Height
    ' Else
    ' Reste  sa valeur (blanc)
  End If

This will make one line of two blue.

You can make a loop in each row reding its cotent and see if you have to color it or not.
Or you can, while you fill each row, put in a boolean Array if the row has to be colored or not (boolean array id is the row number).
I’m sure there are a lot of other solution.

Implement the CellBackgroundPaint and/or the CellTextPaint event.

I don’t think I’ve explained my problem clearly. I want certain rows in the listbox to be of a different background color from the others. I can’t say in advance what row numbers these will be. I have tried the following:

< If row = 0 then
g.foreColor= &cff0000
ElseIf me.text = “Score the Hands” Then
g.ForeColor = &c880000
g.ForeColor = &c999900
end if
g.FillRect 0,0,g.width,g.height>

but it does not work.

in the CellBackGroundPaint event, check the text of the row
Then decide what to do about it
Paint the background
Then return true to say ‘I did it, you don’t need to’

select case me.list (row) //get the text of the row. Use me.cell(row,0) if there is more than one column case "This is special" g.forecolor = &cFF0000 g.fillrect 0,0,g.width,g.height case else g.forecolor = &cFFFFFF g.fillrect 0,0,g.width,g.height end select return true

You may want to use different colors if the row is selected.
And check that row < listcount or you may get nil object exceptions

1 Like


Interesting way of doing it.

I used two if blocks to test for two special words (“Missing” and “Folder”) located in the rightmost column to apply two special colors (pink for Missing and green for Folder) and I had to test row and column to not be out of bounds.

This allows me the use of these two words in the other columns without getting an unwanted colour change.

And, no, I do not think at the benefits, I only wanted to do that: test the presence of these two words in the last column and change the background color(s) accordingly. I realized the benefit afterward ;-:).

Owen: don’t get me wrong. If your ListBox will get specific data (say automobile parts), you can set the backgound colours using keywords for the Motor part, another for the car inside parts, another for the outside car parts, others for accessories, and so on.

In these cases, you may not get undesirable background colour changes.

Just an example.

BTW: you can put your “text tag” in the RowTag property and check that to make a background colour change… or not.

Thanks Jeff and Emile and Thomas. Problem solved!

BTW how do I get code to be shown as in Jeff and Thomas’s examples?

Highlight it in the message and click <> code button.

Thanks Paul.

If you need fill with diferet color in a “listbox” in response to a “info column”, i use this:

Create an event named CellBackgroundPaint.

[code]dim myColumnWithInfo as integer
myColumnWithInfo = 3

dim myDataColumToTest as string

’ Test info exist ?

if me.ListCount > 0 then
if row<me.ListCount then

' Take info
  myDataColumToTest = me.Cell(row,myColumnWithInfo)

 ' Evaluate info
  if myDataColumToTest = "S" then
    //g.ForeColor = &cFF0000 ' Red
    g.ForeColor =&cFFECEC ' Red soft
    g.ForeColor = &cE8FFE8 ' Green soft
  end if
  ' FillColor to all rows
  g.fillrect 0,0,g.width,g.height
  return true
end if

end if