RegEx Syntax HighlighterHere is a version of my code for syntax highlighting in a RichTextBox that incorporates regular expressions.

Download RegEx Syntax Highlighting RichTextBox Source Code

Source Code Listed:

Public Class SyntaxRTB

Inherits System.Windows.Forms.RichTextBox </span>Private Declare Function SendMessage Lib “user32” Alias “SendMessageA” _ (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, _

ByVal lParam As Integer) As Integer </span>Private Declare Function LockWindowUpdate Lib “user32” _

(ByVal hWnd As Integer) As Integer </span>

Private _SyntaxHighlight_CaseSensitive As Boolean = False </span>Friend Words As New DataTable

‘Contains Windows Messages for the SendMessage API call </span>Private Enum EditMessages

LineIndex = 187

LineFromChar = 201

GetFirstVisibleLine = 206

CharFromPos = 215

PosFromChar = 1062

End Enum </span>Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)MyBase.OnTextChanged(e)


End Sub </span>Public Sub New()

Me.AcceptsTab = True </span>


End Sub </span>Function AddSQLSyntax()

ClearSyntaxWords() AddSyntaxWord(</span>“\b(select|text|ntext|date|datetime|order by|” & _

“group by|smalldatetime|cursor|on|as|for|filename|” & _ </span>“database|drop|function|delete|insert|update|int|” & _

“varchar|nvarchar|bit|binary|table|inner|where|from|” & _ </span>“out|procedure|view|trigger|set)\b”, Color.Blue)

AddSyntaxWord(“\b@@identity\b”, Color.Pink)AddSyntaxWord( </span>“\b(in|join|outer|and|or)\b”, Color.Gray) AddSyntaxWord(“\bsp_refreshview\b”, Color.Red)

Return True </span>

End Function </span>Public Function ClearSyntaxWords() Words = New DataTable

'’Load all the keywords and the colors to make them </span>Words.Columns.Add(“Word”)

Words.PrimaryKey = New DataColumn() {Words.Columns(0)}Words.Columns.Add( </span>“Color”)

Return True </span>

End Function </span>Public Function AddSyntaxWord(ByVal strWord As String, ByVal clrColor As Color)Dim MyRow As DataRow

MyRow = Words.NewRow()

MyRow(“Word”) = strWordMyRow( </span>“Color”) = clrColor.Name


Return True </span>

End Function </span>Public Sub ColorRtb()

Dim FirstVisibleChar As Integer </span>Dim i As Integer = 0While i < Me.Lines.Length

FirstVisibleChar = GetCharFromLineIndex(i)

ColorLineNumber(i, FirstVisibleChar)

i += 1

End While </span>

End Sub </span>Public Sub ColorVisibleLines()

Dim FirstLine As Integer = FirstVisibleLine() </span>Dim LastLine As Integer = LastVisibleLine()

Dim FirstVisibleChar As Integer </span>

If (FirstLine = 0) And (LastLine = 0) Then </span>

‘If there is no text it will error, so exit the sub </span>

Exit Sub </span>

Else </span>While FirstLine < LastLine

FirstVisibleChar = GetCharFromLineIndex(FirstLine)

ColorLineNumber(FirstLine, FirstVisibleChar)

FirstLine += 1

End While </span>

End If </span>

End Sub </span>Public Sub ColorLineNumber(ByVal LineIndex As Integer, ByVal lStart As Integer)

Dim i As Integer = 0 </span>Dim SelectionAt As Integer = Me.SelectionStart Dim MyRow As DataRow

Dim MyI As Integer </span>

’ Lock the update </span>LockWindowUpdate(Me.Handle.ToInt32)

MyI = lStart

'’Turn the whole link black before applying RegEx Syntax matching. </span>Me.SelectionStart = MyI

Me.SelectionLength = Lines(LineIndex).Length </span>Me.SelectionColor = Color.Black

'’Check for matches in a particular line number </span>Dim rm As System.Text.RegularExpressions.MatchCollection

Dim m As System.Text.RegularExpressions.Match </span>For Each MyRow In Words.Rows

’”( |^)1.*2( |$)” </span>rm = System.Text.RegularExpressions.Regex.Matches(Me.Text, MyRow(“Word”))

For Each m In rm </span>Me.SelectionStart = m.Index

Me.SelectionLength = m.Length </span>Me.SelectionColor = Color.FromName(MyRow(“color”))

Next </span>

Next </span>

’ Restore the selectionstart </span>Me.SelectionStart = SelectionAt

Me.SelectionLength = 0 </span>Me.SelectionColor = Color.Black

’ Unlock the update </span>


End Sub </span>

Public Function GetCharFromLineIndex(ByVal LineIndex As Integer) As Integer </span>Return SendMessage(Me.Handle, EditMessages.LineIndex, LineIndex, 0)

End Function </span>

Public Function FirstVisibleLine() As Integer </span>Return SendMessage(Me.Handle, EditMessages.GetFirstVisibleLine, 0, 0)

End Function </span>

Public Function LastVisibleLine() As Integer </span>Dim LastLine As Integer = FirstVisibleLine() + (Me.Height / Me.Font.Height)

If LastLine > Me.Lines.Length Or LastLine = 0 Then </span>LastLine = Me.Lines.Length

End If </span>Return LastLine

End Function </span>

Public Property CaseSensitive() As Boolean </span>

Get </span>Return _SyntaxHighlight_CaseSensitive

End Get </span>Set(ByVal Value As Boolean)

_SyntaxHighlight_CaseSensitive = Value

End Set </span>End Property

End Class </span>