How to change the color of a particular line of TextArea?

I have a TextArea that I fill line by line:

TextArea1.Text = TextArea1.Text + EndOfLine + line

How to change the color of a particular line? (if it meets certain conditions) without affecting all the text?
Thanks

I don’t know if you can do that with a textarea (and I think you can’t…)
but I would do something like this with a listbox
with a listbox you can change the color of a line using the paintcellbackground event.

I second @Jean-Yves_Pochez idea, using a DesktopListBox.

But you can change the Style of each character in a TextArea using Styles

BTW: While using the DesktopTextArea, try adding new lines using*:

TextArea1.Text.AddText EndOfLine + line

Just because it’s easier to read (and maybe faster)? :slight_smile:
*DesktopTextArea — Xojo documentation

so it seems you can change the text color, but not the text background color in a textarea.

1 Like

Something like:

ptr = MainWindow.myLogArea

msg    = somestring + EndOfLine
msglen = msg.Length
txtlen = ptr.Text.Length

ptr.AddText (msg)

ptr.SelectionStart     = txtlen                 // Selection is the last line of text
ptr.SelectionLength    = msglen
ptr.SelectionTextColor = &c123456               // some colour

You can change the background of text in a TextArea, but only with MBS plugins.

Thanks @Rick_Araujo,
I tried to do the same but why is my code not working?

Var result As String = sh.Result
Var lines() As String = result.Split(EndOfLine)

For Each line As String In lines
  If line.Contains("ThreatRemediated") Or line.Contains("""status_code"": 23") Then 
    Wnd_XPRLogView.TextArea1.SelectionLength = 0
    Wnd_XPRLogView.TextArea1.SelectionTextColor = Color.Red
    Wnd_XPRLogView.TextArea1.AddText line + EndOfLine
    
  ElseIf line.Contains("""status_code"": 20") And Not line.Contains("NoThreatDetected") Then
    Wnd_XPRLogView.TextArea1.SelectionLength = 0
    Wnd_XPRLogView.TextArea1.SelectionTextColor = Color.Blue
    Wnd_XPRLogView.TextArea1.AddText line + EndOfLine
    
  ElseIf line.Contains("""status_code"": 0") And Not line.Contains("Success") Then 
    Wnd_XPRLogView.TextArea1.SelectionLength = 0
    Wnd_XPRLogView.TextArea1.SelectionTextColor = Color.Blue
    Wnd_XPRLogView.TextArea1.AddText line + EndOfLine

  Else
    Wnd_XPRLogView.TextArea1.SelectionLength = 0
    Wnd_XPRLogView.TextArea1.SelectionTextColor = Color.Black
    Wnd_XPRLogView.TextArea1.AddText line + EndOfLine
  End
Next

Next time send a complete sample that works out of box.

What OS and Xojo version you are using?

Xojo 2023 R4 under macOS Sonoma 14.4

I can’t test on my Mac now, but here is a working sample on Windows 11 Xojo 2024r1.1:

Code:

Var lines() As String = Array( _
   "aaaaaaaaa",_
   "bbbbbbbbbb red bbbb",_
   "ccccccccc",_
   "ddddd blue dddddddd" _
)

me.SelectionLength = 0

For Each line As String In lines

  If line.Contains("red") Then 
    
    me.SelectionTextColor = Color.Red
    
  ElseIf line.Contains("blue") Then
    
    me.SelectionTextColor = Color.Blue
    
  End
  
  me.AddText line + EndOfLine
  
Next

You are not setting .selectionStart and without that it will not work. See my example.

THANKS. But, after numerous tests, it seems that the complexity of the characters stored in my lines (log show) does not allow color management line by line.
Example of a line stored in line() ;

2024-04-29 11:12:45.320637+0200 0x17d5     Default     0x0                  668    0    XProtectRemediatorSnowBeagle: [com.apple.XProtectFramework.PluginAPI:XPEvent.structured] {"caused_by":[],"execution_duration":0.00018799304962158203,"status_code":20,"status_message":"NoThreatDetected"}

Try @Jean-Yves_Pochez recommendation. I once did something similar and created a fake terminal.
This also colors rows based on the content of the rows:

Divide your Text. Split it into Columns. This would be the easy way.

But as @Rick_Araujo mentioned. You could also paint individual areas of a Cell, as you like. But this would be far more complex (not impossible).

Your OP question was changing a line color, but your real intention was imitating a console behavior?

You surely can paint a line with multiple colored segments, but it will have a complex parsing that I don’t know if it is really necessary.

Var lines() As String = Array( _
   "aaaaaaaaa",_
   "bbbbbbbbbb bbbb",_
   "2024-04-29 11:12:45.320637+0200 0x17d5     Default     0x0                  668    0    XProtectRemediatorSnowBeagle: [com.apple.XProtectFramework.PluginAPI:XPEvent.structured] {""caused_by"":[],""execution_duration"":0.00018799304962158203,""status_code"":20,""status_message"":""NoThreatDetected""}",_
   "ddddd dddddddd" _
)

me.SelectionLength = 0

For Each line As String In lines
  
  If line.Contains("""status_code"":20") Then
    
    me.SelectionTextColor = Color.Red
    me.AddText line.Left(31)
    me.SelectionTextColor = Color.Blue
    me.AddText line.Middle(31)
    
  Else 
    
    me.AddText line
    
  End
  
  me.AddText EndOfLine
  
Next
1 Like

deleted text