Listbox CellTextPaint issues

I’m having issues with listbox.

I just want to change the colour of a row and I believe I need to change CellTextPaint?

I have the following in that event handler

g.DrawingColor = &cFF000000 if taskcolour="Black" then g.DrawingColor=&c00FF0000 end if

taskcolour is a global property I have set.

The trouble is it changes the whole listbox text colour each change rather than the current row and I’m tearing my hair out!

What you need is the CellBackgroundPaint-Event.

[code]If Me.SelectedRowIndex = row Then

g.DrawingColor = &cFB720000
g.FillRectangle(0, 0, g.Width, g.Height)
Return True

End If

Return False[/code]

No. That’s for background painting.

[quote=464208:@Rod Pascoe]
The trouble is it changes the whole listbox text colour each change rather than the current row and I’m tearing my hair out![/quote]
What Xojo IDE are you using? I don’t see this behavior in 2019r1.1 and API 1.0. Can you reproduce it in a sample project?

Where is the bit that says 'only do this if it is row x?
Here, I assume that the selected row is affected like this, and all other rows paint black.

if row  = me.listindex  then
g.DrawingColor = &cFF000000
if taskcolour="Black" then
  g.DrawingColor=&c00FF0000
end if
else
g.DrawingColor=&c000000
end if

[quote=464208:@Rod Pascoe]I’m having issues with listbox.

I just want to change the colour of a row and I believe I need to change CellTextPaint?

I have the following in that event handler

g.DrawingColor = &cFF000000 if taskcolour="Black" then g.DrawingColor=&c00FF0000 end if

taskcolour is a global property I have set.

The trouble is it changes the whole listbox text colour each change rather than the current row and I’m tearing my hair out![/quote]

You need to test the row property.

my list box shows text and background by month date and value 1,2 or 3 in first column

Textcolor:

Function CellTextPaint(g As Graphics, row As Integer, column As Integer, x as Integer, y as Integer) Handles CellTextPaint as Boolean
Dim m As Integer
Dim d As New date

If wahl=1 Then
rem Platz zeigen
If column > 0 Then
If row > 0 Then
If listbox2.cell(row,column)=“3” Then
g.ForeColor= Color.red
Else
If listbox2.cell(row,column)=“2” Then
g.ForeColor= Color.Orange
Else
g.ForeColor= Color.Green
End If
End If
Else
g.ForeColor= Color.Yellow
End If
Else
If listbox2.cell(row,column)=Str(jahr) Or listbox2.cell(row,column)=Str(jahr+1) Then
g.ForeColor= Color.red
End If
End If
Else
m=d.Month
If Val(lockedbefore.Text) < m Then // Bereich vor Datum geperrt
// Vergangenheit nicht mehr reservierbar/nderbar - Past no longer reservable / changeable
g.ForeColor= Color.White

Else
  rem  Platz/Monat zeigen nach Datum / Place / month show by date
  If listbox2.cell(row,0)="3" Then
    g.ForeColor= Color.Blue
  Else
    If listbox2.cell(row,0)="2" Then
      g.ForeColor= Color.Orange
    Else
      g.ForeColor= Color.Green
    End If
    
  End If
End If

End If

End Function

Hintergrund Color:
Function CellBackgroundPaint(g As Graphics, row As Integer, column As Integer) Handles CellBackgroundPaint as Boolean

If row < Me.ListCount Then

If Me.cell(row, column ) ="WE" Then 
  g.ForeColor = &cAA0000
  g.fillrect(0,0,g.Width,g.height)
End If

End If

End Function

Is this a bug?

I’ve got the code

if row=lstClientTasks.listindex+1 then if taskcolour="Black" then g.DrawingColor=color.Black end if if taskcolour="Red" then g.DrawingColor=color.Red end if end if

Not very elegant I know but wanted it plain to read for small brains like mine.

I also have a message box in my main loop that is filling the listbox so that the program stops with the message box saying what colour it should be.

This is what happens :-

The program runs, fills in the first line in the listbox in red text which is correct. It pops up a messagebox saying Red. So far so good.
I then click ok on the message box and the next line is filled in BUT not only is this line black (and the message box pops up to say it should be black too) but the red line above has now turned black too.

Am I being really really stupid? (quite possible!)

where is “taskcolour” coming from? if it is NEITHER of those values, the text will be BLACK as that is the default color

It’s a global property that is being set in the loop.

It’s working, as I said above the row DOES print in red at the first pass, the second pass turns it all black not just the current row.

you code is only going to color ONE line RED which ever is lstClientTasks.listindex+1

change your code to use blue or green instead of black… just as a test… and see what happens.
I’m going to bet they STILL turn BLACK

[quote=464303:@Rod Pascoe]he program runs, fills in the first line in the listbox in red text which is correct. It pops up a messagebox saying Red. So far so good.
I then click ok on the message box and the next line is filled in BUT not only is this line black (and the message box pops up to say it should be black too) but the red line above has now turned black too.

Am I being really really stupid? (quite possible!)[/quote]
Don’t use MsgBox to debug, it can be misleading like in this case. Because the MsgBox is closing, it asks the window to paint again, and your listbox, and since you’re using globals things being redrawn are changing color.

A simple and straightforward way to color the text of a cell is to store it’s color value as the CellTag.

[code]lbData.AddRow(“Red Text”)
lbData.CellTag(lbData.LastIndex, 0) = Color.Red

lbData.AddRow(“Black Text”)

lbData.AddRow(“Green Text”)
lbData.CellTag(lbData.LastIndex, 0) = Color.Green
[/code]

Then, in lbData.CellTextPaint

[code]dim vTag as Variant = lbData.CellTag(row, column)
if vTag <> nil then
g.ForeColor = vTag.ColorValue

end
[/code]

Thank you Tim, you nailed it!

Changed (row, column) to (row,0) so that the whole line was red and not just the first cell but other than that you saved what’s left of my hair!

Have 15 internet points on me please :slight_smile:

[quote=464312:@Tim Parnell]Don’t use MsgBox to debug, it can be misleading like in this case. Because the MsgBox is closing, it asks the window to paint again, and your listbox, and since you’re using globals things being redrawn are changing color.

A simple and straightforward way to color the text of a cell is to store it’s color value as the CellTag.

[code]lbData.AddRow(“Red Text”)
lbData.CellTag(lbData.LastIndex, 0) = Color.Red

lbData.AddRow(“Black Text”)

lbData.AddRow(“Green Text”)
lbData.CellTag(lbData.LastIndex, 0) = Color.Green
[/code]

Then, in lbData.CellTextPaint

[code]dim vTag as Variant = lbData.CellTag(row, column)
if vTag <> nil then
g.ForeColor = vTag.ColorValue

end
[/code][/quote]