语法高亮时,RichTextBox会调用

时间:2016-08-13 19:57:09

标签: vb.net richtextbox syntax-highlighting

我正在编写一个IDE,一边研究语法高亮,我遇到了一个非常讨厌的问题。

当我输入内容时,文字闪烁......

这是我的代码:

Dim KeyWords As List(Of String) = New List(Of String)(New String() {"void", "int", "long", "char", "short", "unsigned", "signed", "#include", "#define", "return"})
Dim KeyWordsColors As List(Of Color) = New List(Of Color)(New Color() {Color.Purple, Color.Purple, Color.Purple, Color.Purple, Color.Purple, Color.Purple, Color.Purple, Color.Olive, Color.Olive, Color.Blue})

Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged

    Dim words As IEnumerable(Of String) = RichTextBox1.Text.Split(New Char() {" "c, ".", ",", "?", "!", "(", Chr(13), Chr(10), " "})
    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()) Or KeyWords.Contains("<"), 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

    Dim strings() As String = RichTextBox1.Text.Split(Chr(34))
    Dim count As Integer = 0
    Dim cpart As Integer = 0

    For Each part In strings
        cpart = cpart + 1
        If cpart Mod 2 = 0 Then
            coloringRTB(RichTextBox1, count - 1, part.Length + 2, Color.Olive)
        End If
        count = count + part.Length + 1
    Next

    Dim strings2() As String = RichTextBox1.Text.Split(New Char() {"<", ">"})
    count = 0
    cpart = 0

    For Each part In strings2
        cpart = cpart + 1
        If cpart Mod 2 = 0 Then
            coloringRTB(RichTextBox1, count - 1, part.Length + 2, Color.Olive)
        End If
        count = count + part.Length + 1
    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






Private Sub RichTextBox1_KeyUp(sender As Object, e As KeyPressEventArgs) Handles RichTextBox1.KeyPress
    If e.KeyChar = "{"c Then
        RichTextBox1.SelectedText = "{}"
        RichTextBox1.SelectionStart = RichTextBox1.Text.Substring(0, RichTextBox1.SelectionStart).LastIndexOf("{") + 1
        e.Handled = True
    End If
    If e.KeyChar = "("c Then
        RichTextBox1.SelectedText = "()"
        RichTextBox1.SelectionStart = RichTextBox1.Text.Substring(0, RichTextBox1.SelectionStart).LastIndexOf("(") + 1
        e.Handled = True
    End If
    If e.KeyChar = "["c Then
        RichTextBox1.SelectedText = "[]"
        RichTextBox1.SelectionStart = RichTextBox1.Text.Substring(0, RichTextBox1.SelectionStart).LastIndexOf("[") + 1
        e.Handled = True
    End If
    If e.KeyChar = "'"c Then
        RichTextBox1.SelectedText = "''"
        RichTextBox1.SelectionStart = RichTextBox1.Text.Substring(0, RichTextBox1.SelectionStart).LastIndexOf("'")
        e.Handled = True
    End If

    Dim currentLength = RichTextBox1.Text.Length

End Sub

希望有人可以帮助谢谢^ _ ^

RichTextBox1是richtextbox

0 个答案:

没有答案