Darkmode and ListBox

Bad times for my eye, I had to swap to dark mode or look for the ly (and no fly actually, too cold)…


I forgot, so I wasted a day looking for a bug “why my data does not appears in the ListBox ?"

When in fact, it appears, but it is not “readable” in dark mode.

How do I set the things to display my text in the ListBox when in Dark Mode ?

You need to define your custom colors with Light and Dark modes. If you are using single color for both you get those results.

Will try.

You’ll have to use PaintCellText and set colors that stand out against the backgrounds.
Be sure to include Return True or the text will be doubled in listbox rows.

Select Case column
Case 1
  Var mytext As String = Me.CellTagAt(row, 1)
  Var lines() As String = mytext.Split(EndOfLine)
  Var myColor As Color
  mycolor = If(row Mod 2 = 0, &c13448300, &cFF6E0000)
  
  Var l As Integer = lines.Count
  g.ForeColor = myColor 
  g.DrawText(mytext, 0, (g.TextHeight))
  
End Select

Return True

You can use a ColorGroup to define a colour for light mode and another for dark mode.

Var OddRow as New ColorGroup( &cFF6E0000, &c00CC0000 ) // fist colour for light, second for dark
Var EvenRow as New ColorGroup( &c13448300, &c11223300 )
...
mycolor = If(row Mod 2 = 0, EvenRow, OddRow)
1 Like

That is what I get earlier today:

Function PaintCellText(g as Graphics, row as Integer, column as Integer, x as Integer, y as Integer)

Handles PaintCellText as Boolean
  if IsDarkMode Then
    g.DrawingColor = RGB(0,0,0)
    g.Bold = True
    g.DrawText Me.CellTextAt(0),0,0
  End If
End Function

DesktopListBox.

I set Bold to be able to really read what is on screen. Not tested on standard mode (no dark mode).

With Return True, nothing is draw (!), so I removed it…

Me.CellTextAt(0),0,0

Wouldn’t that draw the text outside of the graphics area?
The x,y co-ordinates are the baseline for drawtext…

If you aren’t seeing any text, try


Handles PaintCellText as Boolean
 if IsDarkMode Then
    g.DrawingColor = RGB(255,0,0)  
   //these colors are silly, and purely to show which mode is detected
else
   g.DrawingColor = RGB(0,0,255)
 //these colors are silly, and purely to show which mode is detected
end if
    g.Bold = True
    g.DrawText Me.CellTextAt(0),0,g.height * 0.9 // draw just off the bottom
    //better would be to center the words by adjusting for the font height 
    // versus the g.height
   return true  // now that we did the letters, dont let the system also draw them
End Function

If all you want to do is change the text color, dont draw the text:


Handles PaintCellText as Boolean
 if IsDarkMode Then
    g.DrawingColor = RGB(255,0,0)  
   //these colors are silly, and purely to show which mode is detected
else
   g.DrawingColor = RGB(0,0,255)
 //these colors are silly, and purely to show which mode is detected
end if
    g.Bold = True
 
   return false  //  let the system draw the words
End Function

No, I get normal displaying; look by yourself:

You are correct for the color; I used Red for test, see it fit, set black and stays there.

So you see text because after your drawing, system will draw the text with its defaults again. As Jeff pointed out, you should usually use the x and y parameters from the PaintCellText event, not (0,0). And return true; otherwise system will still draw on its own afterwards.

I came here because the system does not draw anything in dark mode, mode I am temporary using now.

So, I am starting to not understand at all.

For the record: I removed the PaintCellText Event and fired the project and… no text is displayed unless I select all the text.

Did you read the thread before posting ? (or am I becoming crazy ?)

Just your darkmode listbox isn’t dark mode. You define the text color as black, but it would be more recommendable to use PaintCellBackground too and switch the too bright colors of the cells.

My answer was in response to you saying you did not return true in PaintCellText because nothing would be displayed.

OK, going back to your original post:

In dark mode, the system is painting text as white.
That’s correct.
You can’t see it easily because YOU are changing the background (which would have been black) , to yellow and blue.

Since you are changing the background, it will never be black, and the text should not be allowed to go white.

Your text needs to be black, whether it is dark mode or not.
So your PaintCellText needs to be this and only this:


Handles PaintCellText as Boolean
    g.DrawingColor = RGB(0,0,0)  
    g.Bold = True
    g.DrawText Me.CellTextAt(0),0,g.height * 0.9 
   return true  //do not let the system also draw the letters
End Function

Looks like you misunderstanding things, the system is drawing with white text but because you changed the rows to light blue/yellow, you can’t see the white text.
Maybe zooming in you can see the text?

You have several options:
a) remove the light blue/yellow colors and let the default listbox adjust to light/dark mode
b) handle the text color yourself for dark mode
c) use different colors for your light blue/yellow when in Dark Mode knowing that the text will be Black in Light mode and White in Dark Mode
d) use ColorGroup definitions to handle c)

I hope this makes sense to you.

1 Like

As Alberto wrote, your observation is wrong. Text is always displayed, but because of your rows being colored way too bright, there is not enough contrast to white text until the row is repainted with the (darker) selection color as background.

I don’t see any sense in using DarkMode while text background is defined for white mode compatibility. Either switch off DM or use different background cell colors which contrast with white text.

Now it is clear.

Thank you everybodY for your explanation and patience.

1 Like