Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Syntax Highlighter -- RichTextBox

Using VS 2005 and making a syntax highlighter:

Private Sub rtbHTML_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbHTML.TextChanged
Dim TextCursor As Long = rtbHTML.SelectionStart

Dim Pos As Long = 1, Quote As Boolean = False, QuotePos As Long = 0, QuoteEnd As Long = 0

While Pos <= Len(rtbHTML.Text)
If Mid(LCase(rtbHTML.Text), Pos, 6) = "<html>" Then
rtbHTML.SelectionStart = Pos - 1
rtbHTML.SelectionLength = 6
rtbHTML.SelectionColor = Color.Blue
ElseIf Mid(LCase(rtbHTML.Text), Pos, 6) = "<head>" Then
rtbHTML.SelectionStart = Pos - 1
rtbHTML.SelectionLength = 6
rtbHTML.SelectionColor = Color.Blue
ElseIf Mid(LCase(rtbHTML.Text), Pos, 6) = "<body>" Then
rtbHTML.SelectionStart = Pos - 1
rtbHTML.SelectionLength = 6
rtbHTML.SelectionColor = Color.Blue
ElseIf Mid(LCase(rtbHTML.Text), Pos, 7) = "</body>" Then
rtbHTML.SelectionStart = Pos - 1
rtbHTML.SelectionLength = 7
rtbHTML.SelectionColor = Color.Blue
ElseIf Mid(LCase(rtbHTML.Text), Pos, 7) = "</head>" Then
rtbHTML.SelectionStart = Pos - 1
rtbHTML.SelectionLength = 7
rtbHTML.SelectionColor = Color.Blue
ElseIf Mid(LCase(rtbHTML.Text), Pos, 7) = "</html>" Then
rtbHTML.SelectionStart = Pos - 1
rtbHTML.SelectionLength = 7
rtbHTML.SelectionColor = Color.Blue
ElseIf Mid(LCase(rtbHTML.Text), Pos, 6) = "<input" Then
rtbHTML.SelectionStart = Pos - 1
rtbHTML.SelectionLength = 6
rtbHTML.SelectionColor = Color.Blue
ElseIf Mid(LCase(rtbHTML.Text), Pos, 8) = "<a href=" Then
rtbHTML.SelectionStart = Pos - 1
rtbHTML.SelectionLength = 8
rtbHTML.SelectionColor = Color.Purple
ElseIf Mid(LCase(rtbHTML.Text), Pos, 4) = "</a>" Then
rtbHTML.SelectionStart = Pos - 1
rtbHTML.SelectionLength = 4
rtbHTML.SelectionColor = Color.Purple

ElseIf Mid(LCase(rtbHTML.Text), Pos, 1) = Chr(CLng("&H" & 22)) Then
If Quote = False Then
Quote = True
QuotePos = Pos - 1
ElseIf Quote = True Then
Quote = False
QuoteEnd = Pos
rtbHTML.SelectionStart = QuotePos
rtbHTML.SelectionLength = QuoteEnd - QuotePos
rtbHTML.SelectionColor = Color.Red
End If
End If

Pos += 1
End While

rtbHTML.SelectionStart = TextCursor
rtbHTML.SelectionLength = 0
rtbHTML.SelectionColor = Color.Black
End Sub

I'm making a HTML code editor. Once I have too many color code strings to parse, it gets really laggy. About 20 colors/lines will make it lag soooo bad. How can I get it to be better and non-laggy?

Other ways of syntax highlighting are gladly accepted...
[3563 byte] By [orcfan32] at [2007-11-11 7:53:46]
# 1 Re: Syntax Highlighter -- RichTextBox
http://www.google.com/search?q=windows+forms+syntax+highlighting
Phil Weber at 2007-11-11 21:48:23 >
# 2 Re: Syntax Highlighter -- RichTextBox
I've already Googled it. I can't find anything, so that's why I'm posting..
orcfan32 at 2007-11-11 21:49:23 >
# 3 Re: Syntax Highlighter -- RichTextBox
Basically, every time you set the SelectionColor property, the contents of the RichTextBox has to be reset. That's what's taking so long. I would suggest trying to build the RTF string yourself instead of using RichText's methods (moving through the text and coloring it.)

An easy way to figure out how to build the string is to create an RTF document in WordPad, color some words, save the document, then open it in Notepad to view the text with all the RTF formatting tags visible. With a little reverse engineering, you'll see it's much easier and faster to build the string with formatting tags than it is to keep moving the cursor and coloring text.
FauxQuixote at 2007-11-11 21:50:16 >