VB.net richtextbox 多种颜色到指定的词

VB.net richtextbox multiple colors to specified words

所以我的程序将文本文件读入富文本框。例如,我想要所有单词 "echo" "pause" "setlocal" to be blue"%" to be orange "、"rem" "::" to be green 等等。很多不同的单词有不同的颜色。是的,richtextbox 的想法就是这样读取 .bat notepad++ 之类的文件会根据颜色进行处理。

我想出了如何为一个词设置颜色

Private Sub PreviewRTB_TextChanged(sender As Object, e As EventArgs) Handles PreviewRTB.TextChanged
    PreviewRTB.Multiline = True
    PreviewRTB.ScrollBars = ScrollBars.Vertical

    'Set Colors
    Dim index As Integer = 0
    Dim c_blue As String = "Echo"

    While index <> -1
        index = PreviewRTB.Find(c_blue, index, RichTextBoxFinds.WholeWord)
        If index <> -1 Then
            PreviewRTB.SelectionStart = index
            PreviewRTB.SelectionLength = c_blue.Length
            PreviewRTB.SelectionColor = Color.Blue
            index += c_blue.Length
        End If
    End While

但是我在哪里设置 dim c_blue 下的其余单词?我试过 Dim c_blue As String = "Echo" & "setlocal" & "pause" 也试过在两者之间使用 +,但它不起作用。以及如何为其他单词添加另一种颜色?谢谢!

对于简单的着色,只要文字不是太长,这样就可以了:

Dim KeyWords As List(Of String) = New List(Of String)(New String() {"this", "word", "color"})
Dim KeyWordsColors As List(Of Color) = New List(Of Color)(New Color() {Color.Blue, Color.Red, Color.Green})
Private Sub PreviewRTB_TextChanged(sender As Object, e As EventArgs) Handles PreviewRTB.TextChanged
    Dim words As IEnumerable(Of String) = PreviewRTB.Text.Split(New Char() {" "c, ".", ",", "?", "!"})
    Dim index As Integer = 0
    Dim rtb As RichTextBox = sender 'to give normal color according to the base fore color
    For Each word As String In words
        'If the list contains the word, then color it specially. Else, color it normally
        'Edit: Trim() is added such that it may guarantee the empty space after word does not cause error
        coloringRTB(sender, index, word.Length, If(KeyWords.Contains(word.ToLower().Trim()), KeyWordsColors(KeyWords.IndexOf(word.ToLower().Trim())), rtb.ForeColor))
        index = index + word.Length + 1 '1 is for the whitespace, though Trimmed, original word.Length is still used to advance
    Next
End Sub

Private Sub coloringRTB(rtb As RichTextBox, index As Integer, length As Integer, color As Color)
    Dim selectionStartSave As Integer = rtb.SelectionStart 'to return this back to its original position
    rtb.SelectionStart = index
    rtb.SelectionLength = length
    rtb.SelectionColor = color
    rtb.SelectionLength = 0
    rtb.SelectionStart = selectionStartSave
    rtb.SelectionColor = rtb.ForeColor 'return back to the original color
End Sub

这里的想法很简单:

  1. 列出您要着色的字词
  2. 列出这些词的颜色
  3. 当RTB文本改变时,用String.Split逐字解析(注意条件"RTB text is changed"可以随意改变)
  4. 检查解析的单词。如果找到,请开始为该词着色。如果没有,用默认颜色给它上色
  5. 转到下一个单词,直到文本阅读结束。
  6. 不要忘记根据 word.Length +1 为空格
  7. 增加检查器的当前索引

结果将是这样的:

下一个 (2)

下一个 (3)

下一个 (4)

下一个 (5)

Note: If the text gets too long, consider of creating a timer (or some other module) to periodically check your RTB instead of checking the text when the text changed. Thus, when the text changed. You reset the timer. When the text is not changed after sometimes, then you start to implement the coloring logic for all the text. This way, you make the word-color checker work significantly less.