DesktopTextArea text hightlighting question

Using 2023r4 under macOS Sonoma.

If I use the mouse to select some text in a DesktopTextArea, it has one colour. However, if I set the selected text in code, it uses another colour.

To me, it looks like for a manual selection, this is the highlight colour as set (in macOS) in the Appearance Panel of System Settings, whereas for a selection using code the highlight is a grey that is used when the window loses focus.

Is this deliberate, a bug, or fixable via some setting I have overlooked.

What you’re seeing are the inactive and active text highlight colors. Which is used depends on the control’s focus state. If you add TextArea1.SetFocus before or after setting the Selection properties, the active color will be used. If, however, focus is elsewhere, the inactive color will be used.

Here’s an example. The button contains the following code:

TextArea1.SelectionStart = 4
TextArea1.SelectionLength = 4

TextArea2.SelectionStart = 4
TextArea2.SelectionLength = 4

The first image is with focus in TextArea1, and the second with focus in TextArea2.


1 Like

I tried this; it had no effect. I also just tried this project under Win-10: even worse, no highlighting occurs at all. Trying a Linux VM next.

Edit: works nicely under Mint 21.

Really not sure what might be going on. I set the focus to TextArea1 by clicking, then have the following code in the button:

TextArea1.SelectionStart = 4
TextArea1.SelectionLength = 4

TextArea2.SetFocus
TextArea2.SelectionStart = 4
TextArea2.SelectionLength = 4

And it works as expected in both macOS and Windows for me. Maybe try using Timer.CallLater to call SetFocus after you’ve set the selection?

NOTE: At least on Windows 11 in dark mode, the system doesn’t show selection in inactive TextAreas.

Here’s the project I’m testing with:

Ah - got it. I was setting the focus variously on the action button I was using and then on the window enclosing the textarea, rather than teh textarea itself. Setting the focus on the textarea does it - as you said, either before or after the actual setting code.

Good! I can press on now and thanks for kicking me a bit :slight_smile:

1 Like

Happy to help.