语法高亮错误(仅在行启动时)

syntax highlight error (only at startup of line)

我使用了这段代码(来源 = youtube)

它工作正常,但唯一的问题是它没有突出显示(第 0 行)之后的行的第一个字符

我的代码:

    If e.KeyCode = Keys.Space Or e.KeyCode = Keys.Back Or e.KeyCode = Keys.Enter Then
        Dim selectionlength As Integer = RichTextBox1.SelectionLength
        Dim selectionstart As Integer = RichTextBox1.SelectionStart
        Dim letter As String = String.Empty
        Do Until letter = " " Or RichTextBox1.SelectionStart = 0
            RichTextBox1.SelectionStart -= 1
            RichTextBox1.SelectionLength += 1
            letter = RichTextBox1.Text.Substring(RichTextBox1.SelectionStart, 1)
        Loop
        If RichTextBox1.SelectedText = "Hello" Or RichTextBox1.SelectedText = " Hello" Then
            RichTextBox1.SelectionColor = Color.HotPink
        ElseIf RichTextBox1.SelectedText = "Dinesh" Or RichTextBox1.SelectedText = " Dinesh" Then
            RichTextBox1.SelectionColor = Color.Peru
        Else
            RichTextBox1.SelectionColor = Color.White
        End If
        RichTextBox1.SelectionStart = selectionstart
        RichTextBox1.SelectionLength = 0
        RichTextBox1.SelectionColor = Color.White
    End If

输出为:Screenshot

您可以看到所附屏幕截图的输出。

绿色箭头(我画的)指出单词HelloDinesh没有高亮

我使用了几种技术,比如我最喜欢的 Fastcolored 文本框,但我想使用这种非常简单的方法,因为我的程序不是很大。

请告诉我一些需要更改的地方。

您可以用 vb.net 或 c# 回答。

提前致谢。

这是 window 形式的应用程序,不是 WPF

你的问题是Do Until letter = " " Or RichTextBox1.SelectionStart = 0

使用调试器并查看值的简单步骤将很快揭示这一点。它会向您展示(使用 Hello 这个词)当 selectionstart 为 0 时循环结束,但您仍然需要再次处理该循环,因为在它移回 0 时您只在 e.一个解决方案是将其更改为 Loop Until。

        Do
            RichTextBox1.SelectionStart -= 1
            RichTextBox1.SelectionLength += 1
            letter = RichTextBox1.Text.Substring(RichTextBox1.SelectionStart, 1)
        Loop Until letter = " " Or letter Or = vbLf Or RichTextBox1.SelectionStart = 0

现在你遇到的下一个问题是,在第二行,你不会到达 0 " ",但你想在那里结束搜索,所以你需要包括一个 vbLf 测试(当有人点击时发生的换行符输入移动到新行。

现在您有了一个新的停止点,然后您需要将其包含在更改颜色的检查中

    If RichTextBox1.SelectedText = "Hello" OrElse RichTextBox1.SelectedText = " Hello" OrElse RichTextBox1.SelectedText = vbLf & "Hello" Then
            RichTextBox1.SelectionColor = Color.HotPink
    ElseIf RichTextBox1.SelectedText = "Dinesh" OrElse RichTextBox1.SelectedText = " Dinesh" OrElse RichTextBox1.SelectedText = vbLf & "Dinesh" Then
            RichTextBox1.SelectionColor = Color.Peru
    Else

如您所见,随着您向其中添加新的命令和规则,这将变成一个巨大的代码块。例如,您对标点符号有疑问。在以下示例中,此代码不会为 Dinesh 着色: Hello is your name Dinesh? 因为 ?

我见过的另一种代码格式化方法是根据已知关键字列表扫描用户所在的当前行,然后根据您找到的任何关键字的格式规则来格式化该行。因为有一种方法可以搜索部分文本(即在 Dinesh 中找到 Dinesh?) 这样可以减少代码。

编辑:还有,忘了说了。如果第一个字符是 Space、Enter 或 Backspace,此代码将崩溃。你知道为什么吗?这是一个简单的修复,所以我会把它留给你解决。

这将解决您的问题:

   If e.KeyCode = Keys.Space Or e.KeyCode = Keys.Enter Then
        Dim selstrt As Integer = RichTextBox1.SelectionStart
        Dim charc As String = String.Empty
        Dim first = RichTextBox1.GetFirstCharIndexOfCurrentLine
        Do Until charc = " " Or RichTextBox1.SelectionStart = 0 OrElse RichTextBox1.SelectionStart = first
            RichTextBox1.SelectionStart = RichTextBox1.SelectionStart - 1
            RichTextBox1.SelectionLength = RichTextBox1.SelectionLength + 1
            charc = RichTextBox1.Text.Substring(RichTextBox1.SelectionStart, 1)
        Loop
        If RichTextBox1.SelectedText = "Hello" Or RichTextBox1.SelectedText = " Hello" Then
            RichTextBox1.SelectionColor = Color.HotPink
        ElseIf RichTextBox1.SelectedText = "Dinesh" Or RichTextBox1.SelectedText = " Dinesh" Then
            RichTextBox1.SelectionColor = Color.LightGreen
        Else
            RichTextBox1.SelectionColor = Color.White
        End If
        RichTextBox1.SelectionStart = selstrt
        RichTextBox1.SelectionLength = 0
        RichTextBox1.SelectionColor = Color.White
 End If