#Region " Syntax Coloring RichtextBox1 "
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Declare Function LockWindowUpdate Lib "user32" (ByVal hWnd As Integer) As Integer
Dim FunctionColor As Color = Color.Blue
Dim UserVariableNameColor As Color = Color.DarkOrange
Dim OperatorColor As Color = Color.Red
Dim NumberColor As Color = Color.Black
Dim ConstantColor As Color = Color.LimeGreen
Public Enum EditMessages
LineIndex = 187
LineFromChar = 201
GetFirstVisibleLine = 206
CharFromPos = 215
PosFromChar = 1062
End Enum
Public Sub ColorRtb()
'Colors the entire RichTextbox
Dim FirstVisibleChar As Integer
Dim i As Integer = 0
While i < RichTextBox1.Lines.Length
FirstVisibleChar = GetCharFromLineIndex(i)
ColorLineNumber(i, FirstVisibleChar)
i += 1
End While
If TB.CanFocus Then TB.Focus()
End Sub
Public Sub ColorVisibleLines()
If SyntaxColoring Then
'Colors the visible lines in the 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
Exit Sub
Else
While i < LastLine
FirstVisibleChar = GetCharFromLineIndex(FirstLine)
ColorLineNumber(FirstLine, FirstVisibleChar)
FirstLine += 1
i += 1
End While
End If
End If
End Sub
Public Sub ColorLineNumber(ByVal LineIndex As Integer, ByVal lStart As Integer)
'Colors a single line in the RichTextBox control
Dim Line As String = RichTextBox1.Lines(LineIndex).ToLower
Dim i As Integer = 0
Dim j As Integer = 0
Dim k As Integer = 0
Dim SelectionAt As Integer = RichTextBox1.SelectionStart
Dim str As String
' Lock the update
LockWindowUpdate(RichTextBox1.Handle.ToInt32)
' Color the line black to remove any previous coloring
RichTextBox1.SelectionStart = lStart
RichTextBox1.SelectionLength = Line.Length
RichTextBox1.SelectionColor = Color.Black
'' Find any operators
i = 0
While i < strOp.Count
For j = 0 To Line.Length - 1
If Line.Substring(j, 1) = strOp(i).ToString Then
RichTextBox1.SelectionStart = (lStart + j)
RichTextBox1.SelectionLength = 1
RichTextBox1.SelectionColor = OperatorColor
End If
Next
i += 1
End While
'check for user defined variable names
If Names.Count > 0 Then
i = 0
While i < Names.Count
j = InStr(Line, CStr(Names(i)))
If j <> 0 Then
RichTextBox1.SelectionStart = (lStart + j - 1)
str = CStr(Names(i))
RichTextBox1.SelectionLength = str.Length
RichTextBox1.SelectionColor = UserVariableNameColor
End If
i += 1
End While
End If
i = 0
'check for built in function names
While i < strFunc.Count
j = InStr(Line, CStr(strFunc(i)))
If j <> 0 Then
RichTextBox1.SelectionStart = (lStart + j - 1)
str = CStr(strFunc(i))
RichTextBox1.SelectionLength = str.Length
RichTextBox1.SelectionColor = FunctionColor
End If
i += 1
End While
'check for built in constant names
i = 0
While i < StrConstant.Count
str = CStr(StrConstant(i)) 'get the constants name
j = 0 'reset the pointer
Do While j < Line.Length - 1 'search for the constant
j = Line.IndexOf(str, j) ' get the first instance of the constant
Select Case j
Case Is = -1 'constant not in line
Exit Do
Case Is = 0 'start of the string
'make sure that the constant is not the start of a word such as e in exp
If Line.Substring(j + 1, 1).ToLower Like "[a-z]" Then 'a letter preceeds
'not a constant
Else
RichTextBox1.SelectionStart = (lStart + j)
RichTextBox1.SelectionLength = str.Length
RichTextBox1.SelectionColor = ConstantColor
End If
Case Else
Select Case (j + str.Length) = Line.Length
Case Is = True 'constant is at the end of the line
If Line.Substring(j - 1, 1).ToLower Like "[a-z]" Then 'a letter preceeds
'not a constant
Else
RichTextBox1.SelectionStart = (lStart + j)
RichTextBox1.SelectionLength = str.Length
RichTextBox1.SelectionColor = ConstantColor
End If
Case Is = False 'the constant is somewhere within the string
If Line.Substring(j - 1, 1).ToLower Like "[a-z]" Then 'a letter preceeds
'not a constant
ElseIf Line.Substring(j + str.Length, 1).ToLower Like "[a-z]" Then 'a letter proceeds
'not a constant
Else
RichTextBox1.SelectionStart = (lStart + j)
RichTextBox1.SelectionLength = str.Length
RichTextBox1.SelectionColor = ConstantColor
End If
End Select
End Select
j += str.Length
Loop
i += 1
End While
' Restore the selectionstart
RichTextBox1.SelectionStart = SelectionAt
RichTextBox1.SelectionLength = 0
' Unlock the update
LockWindowUpdate(0)
End Sub
Private Sub RTBTextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
ColorVisibleLines()
If TB.CanFocus Then TB.Focus()
End Sub
Public Function FirstVisibleLine() As Integer
'Uses API to determine the first line in a RichTextbox control
Return SendMessage(RichTextBox1.Handle.ToInt32, EditMessages.GetFirstVisibleLine, 0, 0)
End Function
Public Function LastVisibleLine() As Integer
'Returns the number of lines in a RichTextbox
Dim LastLine As Integer = CInt(FirstVisibleLine() + (RichTextBox1.Height / RichTextBox1.Font.Height))
If LastLine > RichTextBox1.Lines.Length Or LastLine = 0 Then
LastLine = RichTextBox1.Lines.Length
End If
Return LastLine
End Function
Public Function GetCharFromLineIndex(ByVal LineIndex As Integer) As Integer
' Uses API to get the character
Return SendMessage(RichTextBox1.Handle.ToInt32, EditMessages.LineIndex, LineIndex, 0)
End Function
#End Region