Home » Category » Microsoft Visual Basic

Microsoft Visual Basic: Colour coding for Matthew Gates

104| Fri, 30 Nov 2007 08:24:00 GMT| megatron| Comments (2)
Add to a Form with a RichTextBox and Commandbutton. This will color code VB's Key words.

Option Explicit
Const vbDarkBlue = &H800000
Const vbDarkGreen = &H8000&

Private Sub HighlightText(sKeyword As String, iColour As Long)

Dim nStart As Integer, sPrevChar As String, sNextChar As String

nStart = InStr(1, LCase(RichTextBox1.Text), sKeyword)

Do While nStart <> 0
If nStart > 1 Then
sPrevChar = Mid$(RichTextBox1.Text, nStart - 1, 1)
Else
sPrevChar = " "
End If

If Len(RichTextBox1.Text) >= nStart + Len(sKeyword) Then
sNextChar = Mid$(RichTextBox1.Text, nStart + Len(sKeyword), 1)
Else
sNextChar = " "
End If

If (sPrevChar = Chr(32) Or sPrevChar = Chr(13) Or _
sPrevChar = Chr(10) Or sPrevChar = Chr(9) Or sPrevChar = "(") And _
(sNextChar = Chr(32) Or sNextChar = Chr(13) Or _
sNextChar = Chr(10) Or sNextChar = Chr(9) Or sNextChar = "," Or sNextChar = ")") Then
With RichTextBox1
.SelStart = nStart - 1
.SelText = ""
.SelStart = nStart - 1 + Len("[color=blue]") + Len(sKeyword)
.SelText = ""
.SelStart = Len(RichTextBox1.Text)
.SelColor = iColour
End With
End If

nStart = InStr(nStart + Len(sKeyword), LCase(RichTextBox1.Text), sKeyword)
Loop

End Sub

Private Sub Command1_Click()
With RichTextBox1
.SelStart = 0
.SelLength = Len(.Text)
.SelColor = vbBlack
.SelStart = Len(.Text)
End With


' Add more custom words here
HighlightText "if", vbBlue
HighlightText "doevents", vbBlue
HighlightText "then", vbBlue
HighlightText "else", vbBlue
HighlightText "end", vbBlue
HighlightText "with", vbBlue
HighlightText "select", vbBlue
HighlightText "case", vbBlue
HighlightText "private", vbBlue
HighlightText "public", vbBlue
HighlightText "dim", vbBlue
HighlightText "sub", vbBlue
HighlightText "function", vbBlue
HighlightText "do", vbBlue
HighlightText "loop", vbBlue
HighlightText "for", vbBlue
HighlightText "next", vbBlue
HighlightText "each", vbBlue
HighlightText "integer", vbBlue
HighlightText "boolean", vbBlue
HighlightText "long", vbBlue
HighlightText "double", vbBlue
HighlightText "variant", vbBlue
HighlightText "string", vbBlue
HighlightText "single", vbBlue
HighlightText "byte", vbBlue
HighlightText "date", vbBlue
HighlightText "format", vbBlue
HighlightText "cdate", vbBlue
HighlightText "cvar", vbBlue
HighlightText "cbyte", vbBlue
HighlightText "cbool", vbBlue
HighlightText "cint", vbBlue
HighlightText "clng", vbBlue
HighlightText "csng", vbBlue
HighlightText "cdbl", vbBlue
HighlightText "const", vbBlue
HighlightText "option base 1", vbBlue
HighlightText "option base 0", vbBlue
HighlightText "until", vbBlue
HighlightText "while", vbBlue
HighlightText "option explicit", vbBlue
HighlightText "byval", vbBlue
HighlightText "byref", vbBlue
HighlightText "declare", vbBlue
HighlightText "global", vbBlue
HighlightText "true", vbBlue
HighlightText "false", vbBlue
HighlightText "mod", vbBlue
HighlightText "as", vbBlue
HighlightText "lib", vbBlue
HighlightText "open", vbBlue
HighlightText "close", vbBlue
HighlightText "append", vbBlue
HighlightText "output", vbBlue
HighlightText "input", vbBlue
HighlightText "print", vbBlue
HighlightText "write", vbBlue
HighlightText "binary", vbBlue
HighlightText "access", vbBlue
HighlightText "xor", vbBlue
HighlightText "or", vbBlue
HighlightText "and", vbBlue
HighlightText "in", vbBlue
HighlightText "like", vbBlue
HighlightText "typeof", vbBlue
HighlightText "object", vbBlue
HighlightText "ubound", vbBlue
HighlightText "lbound", vbBlue
HighlightText "to", vbBlue

End Sub

Private Sub Form_Resize()
RichTextBox1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub

Keywords & Tags: colour, coding, matthew, gates, microsoft, visual basic, vb

URL: http://www.programmerbase.com/visual-basic/80725/
 
«« Prev - Next »» 2 helpful answers below.
I modified the code different Megatron.
But it's cool, I just needed some words.

Sometimes you have to sacrafice words though.

In = Int = Integer

You get the point, thanks though Megatron.

hbb | Tue, 04 Dec 2007 11:10:00 GMT |

No problem. Also, I'm posting a request in the Forum Feedback for John to turn on the colour coding again. Let's see if we can get some supporters.

hbb | Tue, 04 Dec 2007 11:11:00 GMT |

Microsoft Visual Basic Hot Answers

Microsoft Visual Basic New questions

Microsoft Visual Basic Related Categories