CellBackgroundPaint vs CellTextPaint

I would like to know the difference between CellBackgroundPaint and CellTextPaint.
Under a certain condition I want to display a cell text in red color and set it to bold.

is there a documentation how to use them and when are those events are called ?

[quote=135632:@Johann JFK]I would like to know the difference between CellBackgroundPaint and CellTextPaint.
Under a certain condition I want to display a cell text in red color and set it to bold.

is there a documentation how to use them and when are those events are called ?[/quote]

http://documentation.xojo.com/index.php/ListBox.CellBackgroundPaint
http://documentation.xojo.com/index.php/ListBox.CellTextPaint

The example for CellTextPaint is rather clear.

Both events take place when the display is refreshed, or when the listbox.invalidate occurs, which you can trigger yourself when needed.

thanks Michael

Some important ListBox performance tips:

  • Never call Refresh. It forces an immediate synchronous redraw of the entire ListBox.
  • Use InvalidateCell whenever possible. ListBox tries to redraw only the cells that are dirty, so Invalidate versus InvalidateCell can mean the difference between hundreds of cells redrawing and a handful. Note that changing a cell’s contents automatically invokes InvalidateCell, so it’s not necessary unless you’re doing things ListBox doesn’t know about.
  • If you really need every single cell in the ListBox redrawn, use Invalidate and pass in False for the ‘eraseBackground’ parameter.

And some minor ListBox performance tips if you find your ListBox is being slow:

  • Avoid using gridlines unless you really need them.
  • Avoid customizing the background painting unless you really need to. Implementing the CellBackgroundPaint event requires ListBox to draw backgrounds a single cell at a time instead of an entire row at a time. This isn’t to say that you should never use CellBackgroundPaint, just that you should think about whether or not the custom look you’re going for is worth it.

Does changing a cell’s content to itself call InvalidateCell?

Essentially would this trigger the InvalidateCell event?
lb.cell(row,column) = lb.cell(row,column)

More or less I have code that does this:

Case "Phone" me.Cell(row,column) = formatPhone(me.Cell(row,column))

And I’m wondering if I need to change it to

Case "Phone" dim formattedText = formatPhone(me.Cell(row,column)) if me.cell(row,column) <> formattedText then me.cell(row,column) = formattedText

Excellent question. It does not check to see if the string value has changed before assigning it to the cell and invalidating it.

[quote=135646:@Joe Ranieri] Avoid customizing the background painting unless you really need to. Implementing the CellBackgroundPaint event requires ListBox to draw backgrounds a single cell at a time instead of an entire row at a time. This isn’t to say that you should never use CellBackgroundPaint, just that you should think about whether or not the custom look you’re going for is worth it.

[/quote]

Hmmm I am wondering if i some cases it might be faster to draw the background for the whole row and then call CellBackgroundPaint individually only for the the cells that need it (I think one of the platforms does -or used to- work that way) …

Of course that does make make it obvious that a speed optimization might be to have a RowBackgroundPaint event for the times you want to have the same custom background for all or most cells on a row! (Custom row highlighting comes to mind)

In that case maybe pass in (row as integer, g as Graphics, HasCellBkgPaintCalls as boolean) or (row as integer, g as Graphics, NumCellBkgPaintCalls as Integer)

Perhaps have it return an integer (or enum) where:

0 = Paint the row normally and call CellBackroundPaint only on the cells that need it,
1 = only call CellBackgroundPaint if needed… (Assume we did the drawing in the event )
2 = Paint the Cells individually and call CellbackgroundPaint if needed.

This more or less happens already in the case of a small number of individually invalidated rows. It only really starts to come into play when the entire control is being redrawn. It’s a fairly minor thing and why I listed it last.

[quote=135656:@Karen Atkocius]Of course that does make make it obvious that a speed optimization might be to have a RowBackgroundPaint event for the times you want to have the same custom background for all or most cells on a row! (Custom row highlighting comes to mind)

In that case maybe pass in (row as integer, g as Graphics, HasCellBkgPaintCalls as boolean) or (row as integer, g as Graphics, NumCellBkgPaintCalls as Integer)

Perhaps have it return an integer (or enum) where:

0 = Paint the row normally and call CellBackroundPaint only on the cells that need it,
1 = only call CellBackgroundPaint if needed… (Assume we did the drawing in the event )
2 = Paint the Cells individually and call CellbackgroundPaint if needed.[/quote]

The small performance savings probably isn’t worth the increase in API complexity.

Johann, there is a difference when a row is selected (actually discovered this myself only yesterday)… Here is a screenshot of where I have images painted without any rows being selected…

Here is the same window when I use CellBackgroundPaint (Notice how the picture is hidden by the selection):

Here is the same window when I use CellTextPaint (Notice the shows correctly with selection):

So probably best to use CellTextPaint if you are unsure.

One thing that often comes up is the ability to have a custom background like a watermark or picture for the whole listbox content area that does not scroll. I can fake it, but it slows things down a bit… An event to paint the content area background would be nice.

  • Karen

that explains a lot Alwyn, but how do you display a picture of bitmap in a listbox cell.
I always read something about a canvas which one should use, but how does a canvas work ?

Can some body give me a good definition of a canvas.

The problem is that Xojo is drawing the selection highlight AFTER CellBackgroundPaint is called… If you returned True from CellBackgroundPaint you would see the drawing, but the cell would not be highlighted, which is NOT the effect you want.

I work around that in CellBackgroundPaint drawing the highlight myself and THEN doing the drawing over that …

It would be better if the highlight was drawn before CellBackgroundPaint was drawn or at least g.forecolor was set to the highlightcolor. That is because Xojo does not give you a reliable way to get the highlight color it uses XPlatform…

Xojo.HighlightColor works for windows but on the Mac you need to use declares to get the right color (son’t know about Linux) … The listbox row highlight color really should be exposed to us

  • Karen

[quote=135678:@Johann JFK]that explains a lot Alwyn, but how do you display a picture of bitmap in a listbox cell.
[/quote]

You draw it into the graphics context suppied in either CellBackgroundPaint Or CellTextPaint

[quote]
I always read something about a canvas which one should use, but how does a canvas work ?

Can some body give me a good definition of a canvas.[/quote]

Separate control to do drawing in a defined area… and it also has a bunch of other events so you can use it to build your own controls

thanks Karen, I will try to play around with a canvas to see other options of using it.
at the moment I only used it to display a bitmap (icon).

It will be time well spend Johann. The canvas is a powerful control that can be used for lots of things, even for your own custom controls.

Eugene Dakin recently released a book about the canvas control, that includes contributions from Alain Bailleul. Both these guys are very proficient with the canvas control, and you’ll find loads of good content from their works.

[quote=135670:@Karen Atkocius]One thing that often comes up is the ability to have a custom background like a watermark or picture for the whole listbox content area that does not scroll. I can fake it, but it slows things down a bit… An event to paint the content area background would be nice.

  • Karen[/quote]

If you create a ticket for this Karen, then I’ll assign some of my points towards it.