如果RichTextBox中包含某些文本,则更改该行的前景色

时间:2014-01-06 18:46:52

标签: vb.net winforms richtextbox

这可能是一个简单的答案,但目前还没有找到我。 我的程序打开一个文件,文件确实包含注释,我想把“//”后的所有文本都换成绿色。

我想要做的是这样的事情:(这与在VB.Net中评论的方式完全一样,但是用')

If rtb.contains("//") Then
   'make the text after '//' green
End If

2 个答案:

答案 0 :(得分:0)

假设现有文本,您可以通过Lines数组来检查文本:

For i As Integer = 0 To RichTextBox1.Lines.Length - 1
  Dim s As String = RichTextBox1.Lines(i)
  Dim index As Integer = s.IndexOf("//")
  If index > -1 Then
    Dim length As Integer = s.Length - index
    index += RichTextBox1.GetFirstCharIndexFromLine(i)
    RichTextBox1.Select(index, length)
    RichTextBox1.SelectionColor = Color.Green
  End If
Next
RichTextBox1.Select(0, 0)

答案 1 :(得分:0)

您使用的是文本框吗?如果是这样,您需要使用RichTextbox来执行您想要的操作。 像这样:

enter image description here

'设置selectionstart字符 '设置selecionstart字符后所选字符的数量 '设置选择颜色

编辑:哦,你想要在“\\”字符之后,误读,请使用我认为的@LarsTech答案。