change the color of a line or a word in a richtextbox?
Solution 1
This hopefuly should do the trick for you, for example if the line contains "Processing..."
for(int i=0; i<rtb.Lines.Length; i++)
{
string text = rtb.Lines[i];
rtb.Select(rtb.GetFirstCharIndexFromLine(i), text.Length);
rtb.SelectionColor = colorForLine(text);
}
private Color colorForLine(string line)
{
if(line.Contains("[Processing...]", StringComparison.InvariantCultureIgnoreCase) return Color.Green;
By the way I know you said this is for vb.net, but you shoudl be able to use a convertor to convert your code to vb.net Here is a link for one
I have no idea if this is correct but i think it looks a little bit like this in vb
Private Sub Test()
For Each i As Integer In RichTextBox1.Lines.Length
Dim Text As String = RichTextBox1.Lines(i)
RichTextBox1.Select(RichTextBox1.GetFirstCharIndexFromLine(i), Text.Length)
RichTextBox1.SelectionColor = ColorForLine(Text)
Next
End Sub
Private Function ColorForLine(Line As String) As color
If Line.Contains("Processing", ) Then
Return ColorForLine.Green
End If
End Function
Solution 2
This is probably coming a tad late, but all you need to do is set the selectioncolor to the color you want before appending the text to the richtextbox, then reverting back to the original color after that, e.g
With RichTextBox1
.SelectionColor = Color.Yellow
.AppendText("Processing: ")
.SelectionColor = Color.LimeGreen
.AppendText("el senor de los anillakos.avi" & vbCr)
End With
Solution 3
I realize this is older, but the vb.net code proposed by AltF4_ did not work when I needed a similar solution so I modified it so that it does. The ColorForLine function gives the ability to run multiple tests and return multiple colors depending on desired content.
Private Sub CheckLineColorsOfRichTextBox1()
Dim i As Integer = 0
For Each Line As String In RichTextBox1.Lines
RichTextBox1.Select(RichTextBox1.GetFirstCharIndexFromLine(i), Line.Length)
RichTextBox1.SelectionColor = ColorForLine(Line)
i += 1
Next
End Sub
Private Function ColorForLine(Line As String) As Color
If Line.Contains("Processing: ") Then
Return Color.Yellow
Else
Return Color.LimeGreen
End If
End Function
ElektroStudios
Updated on July 09, 2022Comments
-
ElektroStudios almost 2 years
I can change the color of ONE line and/or ONE word perserving the other colors in the richtextbox?
For example i want to change the line "Processing: ..." to yellow colour, It's possible?
Thankyou for read
-
ElektroStudios over 11 yearsthanks but i've tried 3 converters and all give me an error: -- line 1 col 5: EOF expected
-
AltF4_ over 11 yearsOk, let me figure out the VB equivalant and will let you know,
-
AltF4_ over 11 yearsI added some more code supposibly in VB not sure if it is correct, it may take some tweaking
-
avanek over 11 years
For Each i As String
should beFor Each i As Integer