listbox celltextpaint

How can I paint a cell’s foreground text from a global method?

if theBoolean then
myWindow.myListbox.CellTextpaint. etc <- not possible
end if

You could invalidate the listbox, which would cause everything to be repainted. And in the CellTextPaint event, you could read the data text to be written, from a variable.

Mmm… there must be a way to change this from the ‘outside’…

There is no other way afaik. I remember I once read that “painting” outside an event shouldn’t happen, and can cause issues.

You could use a global array of pictures that are drawn into the event, but can be modified by the global method. It may be slow for a large listbox, though.

Found it in an old forum thread:
When populating the listbox (global method) I store a boolean in the CellTag indicating color
In the CelltextPaint event:

If me.CellTag(row,column) = True then g.ForeColor = &cFF000000 end

Very simple in fact

[quote=134156:@Alexander van der Linden]Found it in an old forum thread:
When populating the listbox (global method) I store a boolean in the CellTag indicating color
In the CelltextPaint event:

If me.CellTag(row,column) = True then g.ForeColor = &cFF000000 end

Very simple in fact[/quote]

You could subclass the listbox and use a property to set the color for the whole listbox, or store the text color directly in the celltag if you want different colors for each cell.

g.forecolor = me.celltag(row,column)

You could also use addHandler to pass off the cellTextPaint to an external module and call invalidateCell to redraw the cell needing a refresh…

Could you give an example?

Keep in mind that Carbon and Windows redraws many more cells than the single one requested with an InvalidateCell(MyRow, MyCol) call. In 2014r1 or 2 they got it right in Cocoa, but Windows does a block. Carbon was left alone because it is being deprecated. They did fix it for Windows initially in one of the betas, but it broke some other things, so they reverted the Windows code back. There is a bug report open on this still, but my gut feeling is it will not be addressed. The performance hit on Windows is minimal, Cocoa was the real problem and they got that fixed. Best thing is do some performance testing and see if it is OK after you implement it.

[quote=134232:@Alexander van der Linden]Could you give an example?

[/quote]
Ah, I just tried it and it doesn’t work (bug?)
What does work, is to create a listbox subclass with an event definition:

Event CellTextPaint(g As Graphics, row as integer, column as integer, x as integer, y as integer) As Boolean Sub ()
and an event handler:

Function CellTextPaint(g As Graphics, row As Integer, column As Integer, x as Integer, y as Integer) As Boolean Return RaiseEvent CellTextPaint(g,row,column,x,y) End Function

In a module I add a global method:

[code]Function myTextPaint(l as customlistbox1,g as graphics, r As integer, c as integer, x as integer, y as integer) As Boolean
g.DrawString(str®+":"+str©,x,y)

Return true
//if there is no special drawing to be done, return false and the listbox will handle it.
End Function
[/code]
Then you can pass off the handler with

AddHandler Listbox1.cellTextPaint, AddressOf myTextPaint