我可以更改一行和/或一个单词的颜色,以保留richtextbox 中的其他颜色吗?
例如,我想将“Processing: ...”行更改为黄色,这可能吗?
谢谢你的阅读
我可以更改一行和/或一个单词的颜色,以保留richtextbox 中的其他颜色吗?
例如,我想将“Processing: ...”行更改为黄色,这可能吗?
谢谢你的阅读
这可能有点晚了,但是您需要做的就是将 selectioncolor 设置为您想要的颜色,然后再将文本附加到richtextbox,然后再恢复为原始颜色,例如
With RichTextBox1
.SelectionColor = Color.Yellow
.AppendText("Processing: ")
.SelectionColor = Color.LimeGreen
.AppendText("el senor de los anillakos.avi" & vbCr)
End With
这希望应该为您解决问题,例如,如果该行包含"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;
顺便说一句,我知道您说这是针对 vb.net,但您应该能够使用转换器将您的代码转换为 vb.net 这是一个链接
我不知道这是否正确,但我认为它在 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
我意识到这是较旧的,但是当我需要类似的解决方案时,AltF4_ 提出的 vb.net 代码不起作用,因此我对其进行了修改以使其起作用。ColorForLine 函数提供了运行多个测试并根据所需内容返回多种颜色的能力。
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