Color a certain row in a listbox

Hey all,
Looking to do something here: I want to color a certain row in a listbox green, but only the row where the first element (column 0) matches a certain string called ydna(primary). To give a little background, I have a list of competitors in a race and want to only highlight YOUR player in green. The listbox has been populated already.

How do I cycle through the rows and only turn green the row with your racer’s name in it?

you must test in the backgroundpaint event of the listbox where you are and change the color there if needed

That’s CellBackgroundPaint.

I know it probably has something to do with CellBackgroundPaint, but what would be example code for doing this? I’m stumped for some reason.

Derek,
Put this in your ListBox’s CellBackgroundPaint event:

[code]// ---------------------------------------- COLORIZE COLUMNS 3 AND 4

Select Case column

Case 3
g.ForeColor = &c009900

Case 4
g.ForeColor = &c990000

End Select[/code]

I want to colorize a certain row, not a column.

If (me.cell(row,0) = _________ ) then
g.ForeColor = ______________
end if

Well excuse me!

or you draw yourself, e.g. with fillrect and return true.

No offense, Richard. :slight_smile:

Mark: I think you’re close, but does that code go in CellBackgroundPaint, and does it check all 21 possible rows for the string? I’m getting “Out of Bounds” error right now.

No problem Derek :slight_smile:

Try this in the CellBackgroundPaint event of the listbox. It will check column 0 of all the listbox rows:

If (me.cell(row,0) = "what ever you want to compare to") then g.ForeColor = &c445566 end if

[quote=146165:@Richard Summers]If (me.cell(row,0) = “what ever you want to compare to”) then
g.ForeColor = &c445566
end if[/quote]

That will result in a OutOfBounds exception.

Try

if row < me.ListCount then If (me.cell(row,0) = "color") then g.ForeColor = &cFF0000 g.FillRect(0, 0, g.Width, g.Height) end if end if

instead

Markus: Yours worked! Thanks a lot guys. This forum alone makes Xojo worth developing in over VB6. :slight_smile:

Well, the trick here is that CellBackgroundPaint is called for ALL rows, even the empty ones.

So you need to restrict the painting to those which do have something in the row aka all the rows where the rowNumber “row” is smaller than the “number of rows” (which is the ListCount)

P.S. You might want to store info like this in the rowTag or the columnTag if the user does not need to see it.

E.g. you could store the color to use in the rowTag with

LB.RowTag( <numberOfRow> ) = SomeColor

for example put this in the open event of a Listbox

[code] for i as integer = 0 to 10
me.AddRow “test”, Str(i), “bla”
next

me.RowTag( 2 ) = &cFF0000
me.RowTag( 4 ) = &cFF00FF
me.RowTag( 7 ) = &cFFFF00
[/code]

and then have this in the CellBackgroundPaint event

[code] if row < me.ListCount then

If Me.RowTag( row ) <> Nil Then
  g.ForeColor = Me.RowTag( row ).ColorValue
  g.FillRect(0, 0, g.Width, g.Height)
End If

end if[/code]

If you want to restrict the color to certain columns then just modify the code accordingly:

if row < me.ListCount then if column = 1 then If Me.RowTag( row ) <> Nil Then g.ForeColor = Me.RowTag( row ).ColorValue g.FillRect(0, 0, g.Width, g.Height) End If end if end if

Great stuff, Markus, thanks!

… should have been

for example put this in the open event of a Listbox with 3 (or more) columns

Been a VERY long day.
Glad you got it fixed :slight_smile: