CellBackgroundPaint return true does nothing

I have a hierarchical listbox where I don’t wont automatic painting of selected row. I have understand that returning true from CellBackgroundPaint does it, but it does nothing. I mean, when I click a row it’s selected with the system color selection.
I have done this before but now I can’t. Does something changed or just my insane state?

From ListBox.CellBackgroundPaint doc page:

Before CellBackgroundPaint fires, some drawing may have taken place. It isn’t safe to assume that the background of the cell is white or that the selection hasn’t already been drawn. If you need the background to be clear, you need to clear the background yourself and then return True.

Draw a filled rectangle covering the whole cell in CellBackgroundPaint

g.FillRect( 0, 0, g.width, g.height )

Return TRUE in the CellTextPaint

I do this:

in ListBox.CellBackgroundPaint

g.foreColor= app.colorBlanco     //it's a constant, white color
g.FillRect 0,0,g.width,g.height

and in ListBox.CellTextPaint

if me.ListIndex=row then
  if NOT me.RowIsFolder(row) then
    g.FillRect (0,0,g.Width,g.Height)

but I get the left margins painted (highlighted) on the selected row that are not folders. The 0,0 parameters for g.FillRect is not real 0,0 for the listbox but for the hierarchical row, so, I steel get the row partial highlighted

That’s because the drawing area for CellTextPaint is smaller than for CellBackgroundPaint.

Return TRUE at the end of the CellTextPaint event.

P.S. I actually wrote about this in the current issue of xDev magazine:


1 Like

yes, it’s the last line of that event, but it’s the same with it or without it

so, it’s not possible to override the selection painting in this case?





Sure it is:

Listbox normal:

ListBox modified selection color in the CellBackgroundPaint event:

  If Me.Selected(row) Then 
    g.ForeColor = REALbasic.HighlightColor
  End If
  g.FillRect 0, 0, g.Width, g.Height

Here is the ListBox code that is for my forthcoming article “Listbox with OOP Part 3” in the next xDev where I walk you through creating this:

1 Like

Thanks Markus. I appreciate your help.
I’ve take a look at your code but the listbox you use is not hierarchical. Maybe I’m not following you but my code is just like you’re telling me.

I can’t override this blue margin in your picture in a hierarchical listbox.

Then something is wrong in your code because there is no problem here:

Show the complete code or provide a demo project. Guessing where you MIGHT go wrong is not effective.

Updated the code a bit - same link still applies:

I commented it enough so that anyone should be able to follow it, otherwise I recommend the article in the next xDev magazine (the previous 9 and the next 6 issues are also included in the 2020 OmegaBundle).

1 Like

Thanks Markus. I’ll take a look. Sorry I didn’t upload a code or demo but I was busy with other things. What I can say is that in Windows the behavior is the expected one buy in Mac Os the Margin are steel visible.

[quote=“Hugo_Hollmann, post:13, topic:55599”]
but in macOS the margins are stll visible.[/quote]

Change them (to -10 or so…)

I’ve already tryed that but nothing changed. The margin are untouchable.

Seriously, what do you expect?

You paint the whole cell in the CellBackgroundPaint event with

g.FillRect (0,0,g.Width,g.Height)

and then you paint over it in a different colour in the CellTextPaint event (that covers a smaller area) with another

g.FillRect (0,0,g.Width,g.Height)

What else do you expect to happen???

Just return TRUE in the CellTextPaint event and do ALL the drawing in the CellBackgroundPaint event as I’ve shown in my example.