vb.net 更改富文本框中的线条或单词的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13583494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
change the color of a line or a word in a richtextbox?
提问by ElektroStudios
I can change the color of ONE line and/or ONE word perserving the other colors in the richtextbox?
我可以更改一行和/或一个单词的颜色以保留 Richtextbox 中的其他颜色吗?
For example i want to change the line "Processing: ..." to yellow colour, It's possible?
例如,我想将“Processing: ...”行更改为黄色,这可能吗?
Thankyou for read
谢谢阅读
回答by Nepaluz
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
这可能有点晚了,但是您需要做的就是将 selectioncolor 设置为您想要的颜色,然后再将文本附加到 Richtextbox,然后恢复到原始颜色,例如
With RichTextBox1
.SelectionColor = Color.Yellow
.AppendText("Processing: ")
.SelectionColor = Color.LimeGreen
.AppendText("el senor de los anillakos.avi" & vbCr)
End With
回答by AltF4_
This hopefuly should do the trick for you, for example if the line contains "Processing..."
这有望为您解决问题,例如,如果该行包含 "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
顺便说一下,我知道你说这是针对 vb.net,但你应该能够使用转换器将你的代码转换为 vb.net 这是一个链接
I have no idea if this is correct but i think it looks a little bit like this in vb
我不知道这是否正确,但我认为它在 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
回答by John C
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.
我意识到这是旧的,但是当我需要类似的解决方案时,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