RichTextBox Syntax Highlighting

Welcome to my guide on how to highlight syntax keywords such as: 'public, new, else, if, etc...'
First we will go through how to do it with VB.NET, I am currently using Visual Basic 2010 Express.


VB.NET Guide:

1. Open up VB and create a new project.
Name:  1.jpg
Views: 4083
Size:  27.4 KB

2. Customize the form to whatever you desire.

3. Add a RichTextBox and set the properties so it looks professional.
Name:  2.jpg
Views: 4017
Size:  28.2 KB

4.Now double click on the RichTextBox to get to the code event of RichTextBox.TextChanged

5. Now replace your current code with this:
Code:
Imports System.Text.RegularExpressions

Public Class Form1

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

Dim tokens As String = "(regex|dim|object|new|string|public|integer|end|auto|double|int|struct|break|else|long|switch|case|enum|register|typedef|char|extern|return|union|const|float|short|unsigned|continue|for|signed|void|default|goto|sizeof|volatile|do|if|static|while|1|2|3|4|5|6|7|8|9|my)"
        Dim rex As New Regex(tokens)
        Dim mc As MatchCollection = rex.Matches(RichTextBox1.Text)
        Dim StartCursorPosition As Integer = RichTextBox1.SelectionStart
        For Each m As Match In mc
            Dim startIndex As Integer = m.Index
            Dim StopIndex As Integer = m.Length
            RichTextBox1.[Select](startIndex, StopIndex)
            RichTextBox1.SelectionColor = Color.Blue
            RichTextBox1.SelectionStart = StartCursorPosition

            RichTextBox1.SelectionColor = Color.Black
        Next
    End Sub
End Class
-- Now lets go over the pieces of code individually.

Code:
Dim tokens As String = "(regex|dim|object|new|string|public|integer|end|auto|double|int|struct|break|else|long|switch|case|enum|register|typedef|char|extern|return|union|const|float|short|unsigned|continue|for|signed|void|default|goto|sizeof|volatile|do|if|static|while|1|2|3|4|5|6|7|8|9|my)"
^^^ This creates a new string with all of the keywords that will be highlighted.


Code:
        Dim rex As New Regex(tokens)
        Dim mc As MatchCollection = rex.Matches(RichTextBox1.Text)
        Dim StartCursorPosition As Integer = RichTextBox1.SelectionStart
^^^ This creates some variables that will be able to search for text in the RichTextBox later.


Code:
For Each m As Match In mc
            Dim startIndex As Integer = m.Index
            Dim StopIndex As Integer = m.Length
            RichTextBox1.[Select](startIndex, StopIndex)
            RichTextBox1.SelectionColor = Color.Blue
            RichTextBox1.SelectionStart = StartCursorPosition

            RichTextBox1.SelectionColor = Color.Black
        Next
^^^ This section sets up a 'For Statement' that will keep searching for the keywords defined in the string: 'tokens', and will hightlight them BLUE with the code:
Code:
RichTextBox1.SelectionColor = Color.Blue
and then set it back to BLACK with:
Code:
RichTextBox1.SelectionColor = Color.Black

[b]Thanks for reading! I hope I could help. If you need ANY help, don't hesitate to ask!

The C# Portion will be made later.


Created by MASON COOPER
Please give credit to me (by linking) if you decide to redistribute this guide!