PaintCellText

PaintCellText(g As Graphics, Row As Integer, Column As Integer, x As Integer, y As Integer) As Boolean

The parameter g is a Graphics object that corresponds to the text drawing area of the cell identified by Row, Column. This does not necessarily correspond to the entire cell content area, for example, if you use a row picture in the cell.

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

Is this last sentence actually still the case? Just now looking at my code which loads up my listbox, I observe that what it actually does is to write a RowTag and two CellTags in every row, and it doesn’t write e.g. a space in each column to force the event to occur.

The listbox functions exactly as I expect it to, with all the drawing being done in the PaintCellText event. This event must therefore be firing for all visible cells, but this appears to me to contradict the documentation.

I want the event to fire so it can look in the rowtag and celltags and decide what to put in the cells. And it does so. In some cells it puts a picture, and text in others. I use PaintCellBackground to indicate a selected row, and I have two different types of selected row, each with its own colour.

And you cannot do that in PaintCellBackground ?

PaintCellText does it perfectly well.

Well - if one of the Xojo gurus (or present or former engineer) warned against doing that for some good and well-explained reason, I’d look into it. But presently it works as expected and on all the three desktop platforms, too.

What I’d really like is an answer to my OP. Because if that sentence is now out of date (well, it contradicts my programming experience, anyway), then I’d create an Issue for a doc change.

A quick test indicates it’s not.

In the opening event of the listbox (plain listbox in a blank project):

for i as Integer=0 to 9
  me.AddRow ""
next

In the PaintCellText event:
g.DrawText row.ToString,x,y

With nothing more than blank lines, the event fires. It’s true it didn’t used to, indeed.

Because, even if the cell itself is empty, one may want to draw “computed text”.

PaintCellBackground is less suited for drawing text (e.g. it lacks the x and y parameters).

I have the impression you think one shouldn’t do things beyond what the documentation/your experience teaches.

With .AddRow “” you are already setting an empty string to the first column.
Do that with a listbox with two columns and you will see only the first column will show anything.
It has been working like that for a long time.

OK - thanks all. I’ll make an Issue later.

Looks like you read the docs and see blank as at least a space (" "). Right?

And maybe that is the confusion, usually blank is not the same as empty.

I guess the docs handle “blank” as “you added a row and is blank (either with a space or empty)”.

I’m I understanding the issue correctly?

Could be, could be. Anyway, I’ll ask that the sentence either be removed, or clarified. I know that documentation is hard. I’ve written a lot in the past, and one wants it to be clear without being overly verbose.

What it’s trying to say is that unlike PaintCellBackground, this event only fires for cells in which you have added something, even if that’s an empty string.

In PaintCellBackground you would need to check whether the row and column values are within list bounds, but in PaintCellText they will always be.

PaintCellBackground behaves the way it does so that something like an AlternatingList can draw rows that haven’t been added yet.

Does that help clarify?

2 Likes

It does, thank you @Tim_Parnell .

Oh, you’re right.
And if you happen to assign even an empty string, like the code below, the event suddenly fire for the additional column:

for i as Integer=1 to 10
  me.AddRow ""
  me.CellTextAt(me.LastAddedRowIndex,1)="" //Empty string so the paint event fires for that column
next

It rather looks like “not assigned” is not the same as “empty” or “ ” (space), the two latter being treated equally.

That’s a good point. But are you sure PaintCellText events for empty rows (added using ListBox.AddRow “”) did fire in far earlier Xojo (RealStudio) versions (e.g. ten years ago)? I seem to recall the need to handle these specifically, because CellTextPaint (the API1 version, at that time) wouldn’t execute for rows with empty strings.
Possibly, AddRow has been modified to never really add an empty string, in the meantime? (but that would break code…)

So the documentation is correct. It distinguishes between never had a value, and has some non-nil value.

2 Likes