Unfortunately your code won't work. Setting the base control's ForeColor
, BackColor
will reset the coloring for everything. In order to change the color of just a subset of the text you have to use the SelectionColor property. To get this to work you'd need to set the selection color and then select the text. Perhaps something like this useful extension method.
Module RichTextBoxExtensions
<Extension>
Public Sub AppendTextWithColor(control As RichTextBox, text As String, color As Color)
control.SelectionColor = color
' Ensure nothing selected
control.SelectionLength = 0
control.AppendText(text)
' Reset
control.SelectionColor = control.ForeColor
End Sub
End Module
Note that there are other Selection...
properties to change other aspects.
To use this method just replace the calls to AppendText
.
TextBox1.AppendTextWithColor("Navy", Color.Navy)
TextBox1.AppendText(" ")
TextBox1.AppendTextWithColor("DarkGreen", Color.DarkGreen)