Listbox CellTextPaint Event

I’m once again stumped by the sequence of when events occur.

I have a listbox showing suspense items (a task list). If a task is overdue I want it to display the text in red. When I run a trace the CellTextPaint event is executed before the method that loads the listbox, but apparently does not run when I load text into the listbox.

This code is in the CellTextPaint Event

[code]
if bLate then
g.ForeColor = Color.Red
else
g.ForeColor = Color.Black
end if

Return False[/code]

This code loads the listbox from data in a RecordSet:

[code]dim sSQL As String
dim sMsg As String
dim i As Integer
dim sToday As String
dim sDue As String

sSQL = “SELECT * FROM Suspense WHERE Completed = 0 ORDER BY DueDate”
rs = dbSQL.SQLSelect(sSQL)
if dbSQL.Error then
sMsg = "Database Error reading Suspense Table - " + dbSQL.ErrorMessage
MsgBox(sMsg)
UpdateLog(CurrentMethodName, sMsg, “E”)
Return
end if

lbSuspense.DeleteAllRows
lbSuspense.ColumnType(3) = Listbox.TypeCheckbox

rs.MoveFirst
i = 0
while not rs.eof
dDueDate = rs.Field(“DueDate”).DateValue
sToday = dToday.ShortDate
sDue = dDueDate.ShortDate
if dDueDate.SQLdate <= dToday.SQLdate then
bLate = True
else
bLate = False
end if
lbSuspense.addRow
lbSuspense.RowTag(i) = rs.Field(“pkRecID”).IntegerValue
lbSuspense.cell( i, 0 ) = left(rs.Field(“DueDate”).StringValue, 10)
lbSuspense.cell( i, 1 ) = rs.Field(“Description”).StringValue
lbSuspense.cell( i, 2 ) = rs.Field(“Priority”).StringValue
if bLate then lbSuspense.Refresh
i = i + 1
rs.MoveNext

wend[/code]

The listbox loads except the text of the late item is not red. And since I would never ever let a task go past due, I forced a test record to be late…

don’t do this

 if bLate then lbSuspense.Refresh

try this instead

 if bLate then lbSuspense.invalidatecell(i,-1)

and isn’t bLate a bit global? or did you want ALL items to be RED if any ONE item was late?

The CellTextPaint event can happen multiple times, not just the first time that you load the data into the row.

You should store the bLate boolean value in the CellTag property of the cell that you want to appear in red.
Then in the CellTextPaint event, get the CellTag at the row and column that is being drawn, and determine the text color from the CellTag value.

bLate is a property on the form. It needs to be visible to the CellTextPaint Event.

[quote] if bLate then lbSuspense.invalidatecell(i,-1)[/quote] Tried this with same result.

[quote]Jared - You should store the bLate boolean value in the CellTag[/quote] - I try this out…

Jared - putting it in the celltag worked

thanks

Now try to understand why it didn’t work before. This is an important concept in event driven / object oriented programming.