Click here to Skip to main content
11,920,641 members (61,375 online)
Click here to Skip to main content
Add your own
alternative version


38 bookmarked

Real Time SynTax Colorizing

, 25 Apr 2004
Rate this:
Please Sign up or sign in to vote.
Aaron shows how to use the RichTextbox for Real Time Syntax Coloring painlessly.

Sample Image - syntaxcolorizing.jpg


There seems to be three ways to do real time syntax coloring. Good, fast, and flicker-free. Pick any two. Matthew Hazlett did an article that covers "good and flicker-free", so I thought some people might find it useful to have simply "good and fast".


Back in January, I worked on HTML syntax coloring. I was amazed by the lack of information on the subject in VB.NET. There was some in VB, but naturally, it didn't upgrade too well (nothing is ever easy). So, I had to make my own. Isn't that how things typically are? I've learned a lot from CodeProject, and would like to give back.

Using the code

The code is pretty much self-explanatory, but the idea behind it works something like this:

  1. Find the number of visible lines.
  2. For each visible line, color it.

Simple enough, eh?

Now, on to the actual code, the first subroutine that finds the number of visible lines is called ColorVisibleLines and it looks something like this:

' Sub: ColorVisibleLines()
' It does what?: Colors the visible lines in the RichTextbox
' Notes: N/A
Public Sub ColorVisibleLines(ByVal rtb As RichTextBox)
    Dim FirstLine As Integer = FirstVisibleLine()
    Dim LastLine As Integer = LastVisibleLine()
    Dim FirstVisibleChar As Integer
    Dim i As Integer = FirstLine

    If (FirstLine = 0) And (LastLine = 0) Then
        ' If there is no text in the control, it will run an error
        ' So, if there isn't any text, just exit
        Exit Sub
        While i < LastLine

            FirstVisibleChar = GetCharFromLineIndex(FirstLine)

            ColorLineNumber(rtb, FirstLine, FirstVisibleChar)

            FirstLine += 1
            i += 1
        End While
    End If
End Sub

As you will notice, this sub calls another sub to do all the actual coloring for us.

' Sub: ColorLineNumber()
' It does what?: Colors a single line in the control
' Notes: This should be fairly simple, it looks to see
'  if there are any comments, colors them. Then, it loops
'  through the keywords defined, and colors them
Public Sub ColorLineNumber(ByVal rtb As RichTextBox, _
        ByVal LineIndex As Integer, ByVal lStart As Integer)
    Dim Line As String = rtb.Lines(LineIndex).ToLower
    Dim i As Integer = 0
    Dim Instance As Integer
    Dim SelectionAt As Integer = rtb.SelectionStart

    ' Lock the update

    ' Color the line black to remove any previous coloring
    rtb.SelectionStart = lStart
    rtb.SelectionLength = Line.Length
    rtb.SelectionColor = Color.Black

    ' Find any comments
    Instance = InStr(Line, "'")

    ' If there are comments, color them
    If Instance <> 0 Then
        rtb.SelectionStart = (lStart + Instance - 1)
        rtb.SelectionLength = (Line.Length - Instance + 1)
        rtb.SelectionColor = Color.Green
    End If

    ' There was a comment, so to aviod coloring over the comment
    '  we exit. Unfortunately, this can present a problem 'cuz
    '  what if the user comments at the end of a line with code on it.
    If Instance = 1 Then
        ' Unlock the update, restore the start and exit
        rtb.SelectionStart = SelectionAt
        rtb.SelectionLength = 0
        Exit Sub
    End If

    ' Loop through all the Keywords
    While i < Words.Length

        ' See if the word is in the Line
        Instance = InStr(Line, Words(i).Word)

        ' If the lines contains the word, color it
        If Instance <> 0 Then
            rtb.SelectionStart = (lStart + Instance - 1)
            rtb.SelectionLength = Words(i).Word.Length
            rtb.SelectionColor = Words(i).Color
        End If

        i += 1
    End While

    ' Restore the selectionstart
    rtb.SelectionStart = SelectionAt
    rtb.SelectionLength = 0

    ' Unlock the update
End Sub

The keywords to be colored are stored in a structure called KeyWords which holds the string to color, and the color to color the string.


All in all, it's not the most efficient way to color things. For example, you may have noticed that the Instr function is used to locate the keywords. Regular Expressions (or RegEx) could be used in lieu of the method currently implemented.

Another downside to this method is flicker. This is a very difficult issue to overcome. Even with the LockWindowUpdate API function, it still flickers (go figure). I am currently attempting to make a non-inherited RichTextBox that will be a clone of Visual Studio's editor. However, it's still a long distance from completion. The control will be free and I'll post it on CodeProject, and it will contain a lot of features such as line numbering, breakpoints, brace-matching, expanding-collapsing regions, and best of all good, fast, and flicker-free real time syntax coloring.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

You may also be interested in...

Comments and Discussions

GeneralMy vote of 2 Pin
Dave Kreskowiak29-Jul-09 4:47
mvpDave Kreskowiak29-Jul-09 4:47 
GeneralMake a class! Pin
zauberfee13-Oct-08 5:27
memberzauberfee13-Oct-08 5:27 
QuestionBackground highlight line if it starts with "fox" Pin
UltraWhack16-Jun-08 5:45
memberUltraWhack16-Jun-08 5:45 
GeneralWeird problems Pin
The Mighty Atom14-Jan-08 4:10
memberThe Mighty Atom14-Jan-08 4:10 
GeneralRe: Weird problems Pin
The Mighty Atom19-Jan-08 9:36
memberThe Mighty Atom19-Jan-08 9:36 
GeneralNice code Pin
IDstrife15-Oct-06 6:07
memberIDstrife15-Oct-06 6:07 
GeneralFirstVisibleLine() Pin
Silberztein28-Oct-05 2:53
memberSilberztein28-Oct-05 2:53 
GeneralExtremely Poor Performance Pin
andrewPP21-Mar-05 20:30
memberandrewPP21-Mar-05 20:30 
GeneralMake it a module Pin
nanbil24-Sep-04 6:04
membernanbil24-Sep-04 6:04 
GeneralRe: Make it a module Pin
Aaron Eldreth6-Oct-04 3:24
memberAaron Eldreth6-Oct-04 3:24 
GeneralInteresting... a little bug though Pin
Ryan Binns14-Sep-04 19:13
memberRyan Binns14-Sep-04 19:13 
GeneralRe: Interesting... a little bug though Pin
Aaron Eldreth15-Sep-04 11:51
memberAaron Eldreth15-Sep-04 11:51 
GeneralRe: Interesting... a little bug though Pin
nanbil24-Sep-04 6:12
membernanbil24-Sep-04 6:12 
GeneralSuggestions for a successful colorizer Pin
caractacus29-Apr-04 2:45
membercaractacus29-Apr-04 2:45 
GeneralRe: Suggestions for a successful colorizer Pin
Aaron Eldreth29-Apr-04 10:39
memberAaron Eldreth29-Apr-04 10:39 
GeneralThis sounds interesting; any references? Pin
Rolf Schaeuble5-May-04 1:28
memberRolf Schaeuble5-May-04 1:28 
GeneralRe: This sounds interesting; any references? Pin
caractacus5-May-04 4:14
membercaractacus5-May-04 4:14 
GeneralRe: This sounds interesting; any references? Pin
Rolf Schaeuble5-May-04 11:07
memberRolf Schaeuble5-May-04 11:07 
GeneralFiles Problem Pin
Dejan Petrovic27-Apr-04 15:20
memberDejan Petrovic27-Apr-04 15:20 
GeneralRe: Files Problem Pin
Aaron Eldreth27-Apr-04 17:07
memberAaron Eldreth27-Apr-04 17:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151120.1 | Last Updated 26 Apr 2004
Article Copyright 2004 by Aaron Eldreth
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid