Controlling listbox selected row color after loosing focus

Can’t seem to get rid of the fugly light grey that fills the selected row after a listbox looses it’s focus… both CellBackgroundPaint and CellTextPaint events are set to return True.

Grateful for advice on how to change this!

As I said in the other forum, post you code and what event it’s in!

-Karen

Isn’t the “grey” color there to mark what is selected in the listbox when it loses focus?

Do you need a different behaivour than that?

1 Like

Hi @Karen, @HMARROQUINC
the code (and feel free to criticise this!) is below. hoveredRow is a property of the subclassed listbox that gets set to the RowByXY on mouseEnter, colours are predefined constants:

CellBackgrounPaint

If row = Me.SelectedRowIndex Then
g.DrawingColor = kBackgroundHiliteLight
Elseif row = Me.hoveredRow Then
g.DrawingColor = kBackgroundHoverLight
Elseif row Mod 2 <> 0 Then
g.DrawingColor = kBackgroundAlternateLighter1
Else
g.DrawingColor = kBackgroundOffWhite
End If

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

CellTextPaint

g.DrawingColor = kTextDark
If Me.SelectedRowIndex = row Then g.DrawingColor = kTextLight
Return True

Apart from the aesthetics of colour - which are awful when the listbox looses focus in my layout - the issue is that this code sets the text colour to light theme because the selected row is dark; but when it looses focus it becomes light (so you end up with light text on a light background, borderline unreadable)

Ideally i would not want the listbox to give the appearance of loosing focus at all or be able to select an appropriate ‘lost focus’ row colour…

@Stam, if all you want is for the listbox to display as if no row is selected, simply set the ListIndex to -1 in the LostFocus event. If you save the actual ListIndex first, you can restore it the next time the ListBox GotFocus event fires.

1 Like

Thanks @Dale. The initial plan was to keep the row highlighted as an indicator, but sounds like it may not be feasible after all, as it sounds like it is not possible to change the selected row colour after loosing focus…

I don’t see why this shouldn’t work. I’ve done this multiple times in the past and never had any issues.

To test your code I’ve just created a new project with Xojo 2019r3.1 on a Mac. The ListBox always displays the normal (non-gray) highlight color for the selected row. It works when the ListBox loses its focus (e.g. when another control Is selected) and even when another window becomes active or the app becomes inactive.

What OS and what project type (desktop, iOS, web, …) are you using?

Yeh got me baffled too, just can’t get it to work.
Target is desktop (macOS + Win) using 2019r3.

For now have just set it to remove all highlighting on mouseExit which is the next best thing I suppose.

But supremely annoying :-/

Curious.

Is the code you posted for CellBackgroundPaint the whole code for that event? Or is there more that may cause your troubles?

Is your subclassed listbox based directly on ListBox, or is there another subclass “in between”? If there is, maybe that other subclass interferes…

I’m using macOS Mojave. Are you on Catalina perhaps? Maybe it’s the OS that causes the troubles?

That’s all I can think of right now…

No, it’s a direct subclass of Listbox with no intermediary classes… There is a bit more code in both events that draws some icons but nothing that applies colour in any way…

I’m on Catalina - so perhaps it’s the OS.

In any case, life is too short to keep faffing with this. I’ve chalked it up to yet another of those things I don’t understand so will just sidestep… So I’ve just set it to remove the highlight on loosing focus…