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
else
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

Jeff:

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
  else
    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
[/code]