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

时间:2016-01-27 06:49:50

标签: .net vb.net text-coloring

所以我的程序将文本文件读入richtextbox。例如,我想要所有单词"echo" "pause" "setlocal" to be blue"%" to be orange","rem" "::" to be green等等。很多不同的词会得到不同的颜色。是的,这个想法是这样的,富文本框读取像记事本++这样的.bat文件是做颜色的。

我已经弄明白了如何将颜色设置为一个单词

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

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

1 个答案:

答案 0 :(得分:1)

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

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解析(请注意条件&#34; RTB文本已更改&#34;可以随时更改)
  4. 检查已解析的单词。如果找到,则开始着色该单词。如果没有,请使用默认颜色为其着色
  5. 转到下一个单词,直到文本阅读完成。
  6. 请勿忘记根据空白的word.Length +1增加检查器的当前索引
  7. 结果将是这样的:

    enter image description here

    下一步(2)

    enter image description here

    下一步(3)

    enter image description here

    下一步(4)

    enter image description here

    下一步(5)

    enter image description here

      

    注意:如果文本太长,请考虑创建一个计时器(或一些   其他模块)定期检查您的RTB而不是检查   文本更改时的文本。因此,当文本改变时。你重置了   计时器。当文本有时没有改变时,你就开始了   为所有文本实现着色逻辑。这样,你做了   字色检查工作明显减少。