crimson589
crimson589

Reputation: 1306

Preserving richtextbox formatting when editing 1 line

I have a program which logs to a richtextbox, the logs are color coded depending on the error/event.

Here's how I add text to my logs with default style.

rtbLogs.AppendText("Log Text")    

Here's how I add text when they're colored.

rtbLogs.Select(rtbLogs.TextLength, 0)
rtbLogs.SelectionFont = New Font(rtbLogs.Font, FontStyle.Regular)
rtbLogs.SelectionColor = Color.Red 'Settings the font styles
rtbLogs.AppendText("Error Text")
rtbLogs.SelectionFont = rtbLogs.Font
rtbLogs.SelectionColor = rtbLogs.ForeColor 'reset style to default

Now sometimes I need to update 1 line in my log, after editing all formatting disappears. After editing the next logs I add with be formatted how I want but disappears again if I edit 1 line.

Here's how I edit a line.

Dim lines() As String = Me.rtbLogs.Lines
lines(5) = "Updated Text"
Me.rtbLogs.Lines = lines

How do I preserve the formatting?

Upvotes: 0

Views: 52

Answers (1)

Slai
Slai

Reputation: 22876

maybe something like this

Me.rtbLogs.Rtf = Me.rtbLogs.Rtf.Replace(Me.rtbLogs.Lines(5), "Updated Text")

Upvotes: 1

Related Questions