RichTextBox查找和颜色文本视觉基础

Jun*_* L. 5 vb.net

嗨,我有一个代码,用于从richtextbox中查找单词并更改字体颜色,代码正常工作但ifi返回并将之前的文本编辑为我不想着色的内容,颜色不会消失.这是我的代码

Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
    Dim S As Integer = RichTextBox1.SelectionStart
    Dim html() As String = {"<!DOCTYPE html>", "<html>", "</html>", "<head>", "</head>", "<body>", "</body>", "pre>", "</pre>", "<!DOCTYPE>", "<title>", "</title>", "<a>",
                            "<abbr>", "<address>", "<area>", "<article>", "<aside>", "<audio>", "<acronym>", "<applet>", "<b>", "<base>", "<bdi>", "<bdo>", "<blockquote>", "<body>", "<br>", "<button>", "<basefont>", "<bgsound>", "<big>", "<blink>"}
    For i As Integer = 0 To html.Length - 1
        Dim str As String = html(i)
        Dim start As Integer = S - str.Length - 1
        If (start >= 0) Then
            If (RichTextBox1.Text.Substring(start, str.Length).ToLower.Equals(str)) Then
                RichTextBox1.SelectionStart = start
                RichTextBox1.SelectionLength = str.Length
                RichTextBox1.SelectionColor = Color.Green
                RichTextBox1.SelectionStart = S
                RichTextBox1.SelectionLength = 0

            End If
        End If
    Next
    RichTextBox1.SelectionColor = RichTextBox1.ForeColor
End Sub
Run Code Online (Sandbox Code Playgroud)

当我运行ВоляАбоСмерть提供的代码时,一半的文本用不同的颜色着色.

在此输入图像描述