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

enter image description here

有帮助吗?

解决方案 2

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

C# to VB

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

其他提示

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

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top