CellBackgroundPaint or CellTextPaint?

Are there any guidelines on when to use which event?

As far as I can see cell’s graphic area accessible to the event is slightly smaller for the CellTextPaint event - it’s (0, 0) is about where (2, 2) is in the CellBackgroundPaint event.

On the other hand with the CellBackgroundPaint event you have to deal with the correct highlighting yourself, but that is easily done.

Anything else to look out for?

Btw in the documentation for CellTextPaint it says:

In order for this event handler to be called, the cell itself must have been given a value, even if it is blank.

but that doesn’t seem to be the case. Adding nothing when adding a row like this

Sub Open() Handles Open
  
  For i As Integer = 0 To 10
    Me.AddRow 
  Next

End Sub

still calls the CellTextPaint event.

Besides the border area you mentioned, the graphics area you get with CellTextPaint considers row picture if any, checkbox if any, as well as indent level for a hierarchical listbox… so it CAN be considerably smaller than what you get in the Cell BackgroundPaint…

I had tower it all out for my listbox subclass.

In nay case it is the only part of teh cell you would normally want to write text in considering all teh love features.

  • karen

Unless something has changed, the docs are (or were) right. It is )or was)true that if there is nothing in a cell ("" is something IIRC), teh event is not called.

Try what you did with a multicolumn listbox.

-Karen

A quite useful usage for the CellBackgroundPaint event exists for macOS in conjunction with VisualEffectViews. If you put a Listbox unmodified on a window carrying an Effect, the CellBackground’s color will override the EffectView.
With a Graphics.ClearRect (and not replying with True), you can have an effected :wink: Listbox with Selected handling still ok.