Text color of the listbox without focus

Hi,

Could you please tell me how to set the text color in a listbox that doesn’t have focus?

In the figure below, the selected (focused) listbox row is white/blue.

Instead in the figure below, the same line appears in Black/blue because at this moment it does not have focus..

How can I choose/change the text color of the unfocused Listbox row?
Black on blue isn’t very clear.
Thanks

I’m sorry I can’t help (don’t have Windows). On mac it changes to black on gray.

Do you have any code on Paint events?

Hi Alberto,

No difference between first and second Listbox.
It’s very strange…

On your first screen, looks like the top listbox has focus and the second listbox shows some cells in black/gray (very similar to my screenshot) but the first one in blue/white.

It seems that you have some code in Paint events and you may need to adjust that. If no code in Paint events, then the difference between Windows and Mac is more than I expected.

Windows acts the same as Mac in this regard. Definitely a paint issue.

You should be able to test the window.focus property to determine if the listbox has focus or not.

if self.focus = me then
   // has focus
else
   // not focus
end
2 Likes

Is your issue about the focus, or rather about the selected row? I suspect it is the latter.

You didn’t answer whether you used the paint event. On a DesktopListBox there are actually three paint events, called PaintCellBackground, PaintCellText, and PaintDisclosureWidget, of which the first two are relevant for your issue.

If you use any of those two, I recommend to just let the system do it’s own thing for selected rows by adding this code near the top, before you did any drawing related to the issue:

If me.RowSelectedAt(row) Then Return False

If you don’t use those, the issue is likely with you Windows Theme. In that case,

  • just ignore it and tell the users to select a different theme
  • create and use your own theme in your app
  • or use defines to query whatever color is used for selected rows, calculate it’s brightness or other color properties in order to calculate a color with sufficient contrast, which you then can use in PaintCellText
1 Like

Hi Stefan,
Yes, I have this code inside the PaintCellBackground event of both Listboxes.

g.DrawingColor = rgb(236,233,216)

If row >= Me.RowCount Then Return False

If Me.RowSelectedAt(row) Then
g.DrawingColor = Color.Blue
Else
g.DrawingColor = rgb(236,233,216)

End If

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

If column = 0 Then // prima colonna
g.DrawingColor = Color.Blue
g.FillRectangle(0, 0, g.Width, g.Height)
Return True
End If

Once this was removed, everything returned to normal.
So, my question is:
How can I simply change the background and text of a row under a certain condition?
In this particular case, I’d like the text in the first column of a listbox to always be highlighted, choosing the text and background color myself.
Could you give me some guidance on what to write?

Thanks

I am not sure what your screenshots above really depict. From the code in your latest post and it’s accompanying description I think you should mirror your code to the PaintCellText event, swap the colors from background to foreground colors and replace the FillRectangle calls with

g.DrawText(me.CellTextAt(row, column))

the idea being to set the foreground colors yourself, for those cells for which you painted the background yourself.

Is not clear what you want. Something like this (no difference between focus and not-focus listbox?):

if so, the only thing missing is writing white text when the row is selected and the column is 0. If you need something else and you get stuck, let us know.

Hi Alberto,

This is exactly what I want.
Being able to get a different background for a column (in this case, the first) of a Listbox.

I think your code is ok, you just need to paint text white if is blue background.
Do you think you can figure that out?

Yes I fixed it
Thanks

That is what I described in my previous post.

There’s an error there, though. The line of code I gave misses some parameters. It should be:

g.DrawText(me.CellTextAt(row, column), x, y)

Correct.

That is why I asked OP if needed more help as all the information needed is here.