Highlight text in a textarea

Hi. Is it possible to highlight a string of text in a textarea where the highlight is like a yellow or other color like what you can do in a Word document? I have been able to “highlight” the selected text but am changing only the color of the text and would rather have it highlighted similar to using a highlight marker. Is this possible?

I am selecting a specific string using MouseDown and MouseUp, then in the SelChange, I am using the following code:

if me.TextColor = &c000000 then me.SelTextColor = &cFF0000 if me.SelLength > 0 then highlightStrAnswer = me.SelText end if end if

StyleRun is how you get manage styled text in a Xojo TextArea.
Unfortunately it does not look like there is a Highlight Color.

My control, HTML Edit uses Quill which offers a text background color.
However, I disabled it by default because I think it’s hideous in any situation. To enable it, one would have to get a source license and edit the HTML to turn it back on.

Text Input Canvas has no documentation, so I’m not sure if that would work for you.
Formatted Text Control from BKeeney has selectionBackgroundColor which you could set to yellow.

There is another way. Instead of coloring the text itself, place a canvas on top of the TextArea and display a transparent color over the portion of text.

What you need is a way to know where to start the painting.

This method will tell your canvas where to start the painting from the position of the text :

Function XYFromPosition(Position as Integer) As XY dim result as XY Result.X = -1 Result.Y = -1 for Y as Integer = 0 to TextArea1.Height for X as Integer = 0 to TextArea1.Width if TextArea1.InsertionPosAtXY (X, Y) = Position then result.X = X result.y = y return result end if next next Return result End Function

XY is a structure
X as Integer
Y as Integer

If X or Y is -1 it means the requested position is not visible.

You can also use an integer array instead if you are uncomfortable with structures.

Then to know the width of the color rectangle, use g.stringwidth with the same font and font size as the text.

If on Mac, I just noticed Will Shank mentioned MacOSLib contains the means to change text background color at

Hi guys. Thank you for the replies.

Tim - I like the FTC control but not sure I am ready to pay $150 just for that purpose

Michel - Can you tell me more on how to utilize the canvas action? I placed the canvas over the textarea and added in the function, but what am I doing in the canvas? Does something go into Paint or MouseDrag?

Unfortunately the MacOSLib wouldn’t work as I will be having this on Win and Mac

Better than explanations, here is the short test project I used :


Awesome thank you! I will give this a try later today or tomorrow

You’re welcome.

[quote=245520:@Michel Bujardet]Better than explanations, here is the short test project I used :


On Linux,
The canvas prevents selecting text in the textarea.

[quote=245574:@Axel Schneider]On Linux,
The canvas prevents selecting text in the textarea.[/quote]

Unfortunately, each platform has its own way of dealing with events hierarchy.

I just played with my project on Windows. It works. However, I think it is possible to do better, using a transparent TextArea, and placing the canvas underneath.

For Windows : https://forum.xojo.com/19129-transparent-background-textarea/0

For Mac, I think the declare will be better.

Also, my small demo works with only one outlined area. Managing several and dealing with scroll can be somewhat challenging. I looked again for declares, but could not find any. Worse, in Msdn only .net controls are listed. Norman posted the address of Win32 controls, but I can’t locate that at the moment. With some luck, there is some native way that can be activated through declares.

There is nothing in Stack Overflow, though, so I am not too hopeful.

[quote=245520:@Michel Bujardet]Better than explanations, here is the short test project I used :

This is great Michel, thank you! I can definitely work with this