EDN Admin
Well-known member
Hi guys,
Im building an app that has an sql editor with syntax highlighting and intellisense. Ive got the frame, but im stuck with the re-coloring of the RTB. - NOT performance wise.... got that covered... but with the focus inside the RTB.
Suppose the RTB has got a large volume of lines. when i start editing, the on-textchange event triggers the re-coloring of the current line. using;
- selectionStart
- selectionLength
- selectionColor
I am locking the gui, and i am resetting the focus properly. BUT,.. here it comes, ... when im editing at the bottom of the text file, the text in the RTB jumps up...
Picture 1, initial state begin
<img src="http://i978.photobucket.com/albums/ae264/jb_theman/Clipboard02-4.png" border="0" alt="
Picture 2, start editing on line 5
<img src="http://i978.photobucket.com/albums/ae264/jb_theman/Clipboard04-1.png" border="0" alt="
Picture 3, text jumps up, focus is on the right line, but now it is the first one that is displayed.
<img src="http://i978.photobucket.com/albums/ae264/jb_theman/Clipboard05-1.png" border="0" alt="
Code
<div style="color:black; background-color:white
<pre><span style="color:blue Imports System.Text.RegularExpressions
<span style="color:blue Public <span style="color:blue Class SQLRTB
<span style="color:blue Inherits System.Windows.Forms.RichTextBox
<span style="color:blue Public DefaultColor <span style="color:blue As Color
<span style="color:blue Private pDt <span style="color:blue As DataTable
<span style="color:blue Private pCnStr <span style="color:blue As <span style="color:blue String
<span style="color:blue Private cn <span style="color:blue As OleDb.OleDbConnection
<span style="color:blue Private Words <span style="color:blue As <span style="color:blue New DataTable
<span style="color:blue Private <span style="color:blue Declare <span style="color:blue Function SendMessage <span style="color:blue Lib <span style="color:#a31515 "user32" <span style="color:blue Alias <span style="color:#a31515 "SendMessageA" (<span style="color:blue ByVal hWnd <span style="color:blue As IntPtr, <span style="color:blue ByVal wMsg <span style="color:blue As <span style="color:blue Integer, <span style="color:blue ByVal wParam <span style="color:blue As <span style="color:blue Integer, <span style="color:blue ByVal lParam <span style="color:blue As <span style="color:blue Integer) <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Private <span style="color:blue Declare <span style="color:blue Function LockWindowUpdate <span style="color:blue Lib <span style="color:#a31515 "user32" (<span style="color:blue ByVal hWnd <span style="color:blue As <span style="color:blue Integer) <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Private LastChars(2) <span style="color:blue As <span style="color:blue Integer
<span style="color:green <summary>
<span style="color:green Get or sets the connectionstring to the database
<span style="color:green Used for:
<span style="color:green - Intellisense
<span style="color:green - Status updates
<span style="color:green </summary>
<span style="color:green <value></value>
<span style="color:green <returns>Connectionstring to the database</returns>
<span style="color:green <remarks></remarks>
<span style="color:blue Public <span style="color:blue Property ConnectionString() <span style="color:blue As <span style="color:blue String
<span style="color:blue Get
<span style="color:blue Return pCnStr
<span style="color:blue End <span style="color:blue Get
<span style="color:blue Set(<span style="color:blue ByVal value <span style="color:blue As <span style="color:blue String)
<span style="color:blue Try
<span style="color:blue Using cn <span style="color:blue As <span style="color:blue New OleDb.OleDbConnection(value)
cn.Open()
pCnStr = value
fillIntellisensetable()
<span style="color:blue End <span style="color:blue Using
<span style="color:blue Catch ex <span style="color:blue As Exception
cn = <span style="color:blue Nothing
<span style="color:blue End <span style="color:blue Try
<span style="color:blue End <span style="color:blue Set
<span style="color:blue End <span style="color:blue Property
<span style="color:green Keep polling the entered characters to look for an intelli-event
<span style="color:blue Protected <span style="color:blue Overrides <span style="color:blue Sub OnKeyDown(<span style="color:blue ByVal e <span style="color:blue As System.Windows.Forms.KeyEventArgs)
<span style="color:blue MyBase.OnKeyDown(e)
<span style="color:green a = 65
<span style="color:green z = 90
<span style="color:green 0 = 96
<span style="color:green 9 = 105
<span style="color:green 32 = space
<span style="color:green [65 - 90] [96 - 105]
<span style="color:green if it was a "." (dot) try to find a table that matches the previous word
<span style="color:green check pressed key for its range
<span style="color:blue If (e.KeyValue >= 65 <span style="color:blue And e.KeyValue <= 90) <span style="color:blue Or (e.KeyValue >= 96 <span style="color:blue And e.KeyValue <= 105) <span style="color:blue Or e.KeyValue = 32 <span style="color:blue Then
LastChars(0) = LastChars(1)
LastChars(1) = LastChars(2)
LastChars(2) = e.KeyData
<span style="color:blue If LastChars(0) = 32 <span style="color:blue And LastChars(1) <> 32 <span style="color:blue And LastChars(2) <> 32 <span style="color:blue Then
<span style="color:green if the user typed a space and two characters in one go, look for objects
<span style="color:green that start with the last two letters
<span style="color:green set location of intellisense window
<span style="color:blue Dim pnt <span style="color:blue As <span style="color:blue New System.Drawing.Point
<span style="color:blue With pnt
.X = <span style="color:blue Me.PointToScreen(<span style="color:blue Me.GetPositionFromCharIndex(<span style="color:blue Me.SelectionStart())).X
.Y = <span style="color:blue Me.PointToScreen(<span style="color:blue Me.GetPositionFromCharIndex(<span style="color:blue Me.SelectionStart())).Y + <span style="color:blue Me.FontHeight
<span style="color:blue End <span style="color:blue With
<span style="color:blue Dim intll <span style="color:blue As <span style="color:blue New IntelliSense(pDt, Chr(LastChars(1)) & Chr(LastChars(2)), <span style="color:blue Me, pnt)
<span style="color:blue End <span style="color:blue If
<span style="color:blue Else
LastChars(0) = 0
LastChars(1) = 0
LastChars(2) = 0
<span style="color:blue End <span style="color:blue If
<span style="color:blue End <span style="color:blue Sub
<span style="color:green control + v fires a -complete- re-coloring
<span style="color:blue Protected <span style="color:blue Overrides <span style="color:blue Function ProcessCmdKey(<span style="color:blue ByRef m <span style="color:blue As System.Windows.Forms.Message, <span style="color:blue ByVal keyData <span style="color:blue As System.Windows.Forms.Keys) <span style="color:blue As <span style="color:blue Boolean
<span style="color:green Use this to catch paste events, if text was pasted, refresh the whole RTB
<span style="color:green otherwise the update for the current line is good enough
<span style="color:blue Dim KeyDown <span style="color:blue As <span style="color:blue Integer = &H100
<span style="color:blue Dim SystemKeyDown <span style="color:blue As <span style="color:blue Integer = &H104
<span style="color:blue If m.Msg = KeyDown <span style="color:blue Or m.Msg = SystemKeyDown <span style="color:blue Then
<span style="color:blue Select <span style="color:blue Case keyData
<span style="color:blue Case Keys.Control <span style="color:blue Or Keys.V
<span style="color:blue Me.Paste()
RefreshColors()
<span style="color:green If Clipboard.GetDataObject().GetDataPresent(DataFormats.Text, True) Then
<span style="color:green Me.Paste()
<span style="color:green ColorVisibleLines()
<span style="color:green ColorSpecialElements()
<span style="color:green End If
<span style="color:blue End <span style="color:blue Select
<span style="color:blue End <span style="color:blue If
<span style="color:blue End <span style="color:blue Function
<span style="color:green Contains Windows Messages for the SendMessage API call
<span style="color:blue Private <span style="color:blue Enum EditMessages
LineIndex = 187
LineFromChar = 201
GetFirstVisibleLine = 206
CharFromPos = 215
PosFromChar = 1062
<span style="color:blue End <span style="color:blue Enum
<span style="color:blue Public <span style="color:blue Enum IntelliSenseIcons
IDatabase = 0
ITable = 1
IColumn = 2
<span style="color:blue End <span style="color:blue Enum
<span style="color:green This fires the coloring while typing
<span style="color:blue Protected <span style="color:blue Overrides <span style="color:blue Sub OnTextChanged(<span style="color:blue ByVal e <span style="color:blue As System.EventArgs)
ColorLineNumber(<span style="color:blue Me.GetLineFromCharIndex(<span style="color:blue Me.SelectionStart()), <span style="color:blue Me.GetFirstCharIndexFromLine(<span style="color:blue Me.GetLineFromCharIndex(<span style="color:blue Me.SelectionStart())))
ColorSpecialElements()
<span style="color:blue End <span style="color:blue Sub
<span style="color:green repaints all and colors
<span style="color:blue Public <span style="color:blue Sub RefreshColors()
<span style="color:blue Try
<span style="color:blue Dim lncnt <span style="color:blue As <span style="color:blue Integer = 0
<span style="color:blue For <span style="color:blue Each ln <span style="color:blue In <span style="color:blue Me.Lines
ColorLineNumber(lncnt, GetCharFromLineIndex(lncnt))
lncnt += 1
<span style="color:blue Next
ColorSpecialElements()
<span style="color:blue Catch ex <span style="color:blue As Exception
Debug.Print(ex.ToString)
<span style="color:blue End <span style="color:blue Try
<span style="color:blue End <span style="color:blue Sub
<span style="color:blue Public <span style="color:blue ReadOnly <span style="color:blue Property GetLineIndexCurrentEdit() <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Get
<span style="color:blue Return <span style="color:blue Me.GetLineFromCharIndex(<span style="color:blue Me.SelectionStart())
<span style="color:blue End <span style="color:blue Get
<span style="color:blue End <span style="color:blue Property
<span style="color:blue Public <span style="color:blue ReadOnly <span style="color:blue Property GetCharIndexCurrentEdit() <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Get
<span style="color:blue Me.GetFirstCharIndexFromLine(<span style="color:blue Me.GetLineFromCharIndex(<span style="color:blue Me.SelectionStart()))
<span style="color:blue End <span style="color:blue Get
<span style="color:blue End <span style="color:blue Property
<span style="color:green <summary>
<span style="color:green Re-paints all visible lines
<span style="color:green </summary>
<span style="color:green <remarks></remarks>
<span style="color:blue Public <span style="color:blue Sub ColorVisibleLines()
<span style="color:blue Dim FirstLine <span style="color:blue As <span style="color:blue Integer = FirstVisibleLine()
<span style="color:blue Dim LastLine <span style="color:blue As <span style="color:blue Integer = LastVisibleLine()
<span style="color:blue Dim FirstVisibleChar <span style="color:blue As <span style="color:blue Integer
<span style="color:blue If (FirstLine = 0) <span style="color:blue And (LastLine = 0) <span style="color:blue Then
<span style="color:green If there is no text it will error, so exit the sub
<span style="color:blue Exit <span style="color:blue Sub
<span style="color:blue Else
<span style="color:blue While FirstLine < LastLine
FirstVisibleChar = GetCharFromLineIndex(FirstLine)
ColorLineNumber(FirstLine, FirstVisibleChar)
FirstLine += 1
<span style="color:blue End <span style="color:blue While
<span style="color:blue End <span style="color:blue If
<span style="color:blue End <span style="color:blue Sub
<span style="color:blue Public <span style="color:blue Sub ColorLineNumber(<span style="color:blue ByVal LineIndex <span style="color:blue As <span style="color:blue Integer, <span style="color:blue ByVal lStart <span style="color:blue As <span style="color:blue Integer)
<span style="color:blue Try
<span style="color:blue If FirstVisibleLine() = 0 <span style="color:blue And LastVisibleLine() = 0 <span style="color:blue Then
<span style="color:blue Exit <span style="color:blue Sub
<span style="color:blue Else
<span style="color:blue Dim i <span style="color:blue As <span style="color:blue Integer = 0
<span style="color:blue Dim SelectionAt <span style="color:blue As <span style="color:blue Integer = <span style="color:blue Me.SelectionStart
<span style="color:blue Dim MyRow <span style="color:blue As DataRow
<span style="color:blue Dim Line() <span style="color:blue As <span style="color:blue String, MyI <span style="color:blue As <span style="color:blue Integer, MyStr <span style="color:blue As <span style="color:blue String
DefaultColor = Color.Black
<span style="color:green Lock the update
LockWindowUpdate(<span style="color:blue Me.Handle.ToInt32)
MyI = lStart
Line = Split(IgnoreControlCharacters(<span style="color:blue Me.Lines(LineIndex).ToString), <span style="color:#a31515 " ")
<span style="color:green Do keywords
<span style="color:blue For <span style="color:blue Each MyStr <span style="color:blue In Line
<span style="color:blue Me.SelectionStart = MyI
<span style="color:blue Me.SelectionLength = MyStr.Length
<span style="color:blue If Words.Rows.Contains(MyStr) <span style="color:blue Then
MyRow = Words.Rows.Find(MyStr)
<span style="color:blue Me.SelectionColor = Color.FromName(MyRow(<span style="color:#a31515 "Color"))
<span style="color:blue Else
<span style="color:blue Me.SelectionColor = DefaultColor
<span style="color:blue If Mid(MyStr, 1, 1) = <span style="color:#a31515 "@" <span style="color:blue Then
<span style="color:blue Me.SelectionColor = Color.Magenta
<span style="color:blue End <span style="color:blue If
<span style="color:blue End <span style="color:blue If
MyI += MyStr.Length + 1
<span style="color:blue Next
<span style="color:green Restore the selectionstart
<span style="color:blue Me.SelectionStart = SelectionAt
<span style="color:blue Me.SelectionLength = 0
DefaultColor = Color.Black
<span style="color:blue Me.SelectionColor = Color.Black
LockWindowUpdate(0)
<span style="color:blue End <span style="color:blue If
<span style="color:blue Catch ex <span style="color:blue As Exception
<span style="color:green Unlock the update
LockWindowUpdate(0)
<span style="color:blue End <span style="color:blue Try
<span style="color:blue End <span style="color:blue Sub
<span style="color:blue Public <span style="color:blue Sub ColorSpecialElements()
<span style="color:blue Try
<span style="color:blue If FirstVisibleLine() = 0 <span style="color:blue And LastVisibleLine() = 0 <span style="color:blue Then
<span style="color:blue Exit <span style="color:blue Sub
<span style="color:blue Else
LockWindowUpdate(<span style="color:blue Me.Handle.ToInt32)
<span style="color:blue Dim i <span style="color:blue As <span style="color:blue Integer = 0
<span style="color:blue Dim SelectionAt <span style="color:blue As <span style="color:blue Integer = <span style="color:blue Me.SelectionStart
<span style="color:blue Dim tSelectionStart <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Dim tSelectionLength <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Dim tLineIndex <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Dim tIndexFirstCharOnLine <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Dim tLineLength <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Dim rgl <span style="color:blue As <span style="color:blue String = <span style="color:blue Me.Text
<span style="color:green check for strings using regex
<span style="color:blue Dim Quotes <span style="color:blue As MatchCollection
<span style="color:blue Dim redColoring <span style="color:blue As <span style="color:blue Boolean = <span style="color:blue True
<span style="color:green re-using variables can cause hours of debugging -trust me-
tSelectionStart = <span style="color:blue Nothing
tSelectionLength = <span style="color:blue Nothing
<span style="color:blue Dim QuoteCounter <span style="color:blue As <span style="color:blue Integer = 0
Quotes = Regex.Matches(rgl, <span style="color:#a31515 "(?<q>)", RegexOptions.None)
<span style="color:green if there arent any string quantifiers, dont impact the performance
<span style="color:blue If Quotes.<span style="color:blue Count > 0 <span style="color:blue Then
<span style="color:blue For <span style="color:blue Each quote <span style="color:blue As Match <span style="color:blue In Quotes
QuoteCounter += 1
<span style="color:blue If redColoring = <span style="color:blue True <span style="color:blue Then
tSelectionStart = quote.Index
<span style="color:blue Me.SelectionStart = tSelectionStart
<span style="color:blue If QuoteCounter = Quotes.<span style="color:blue Count <span style="color:blue Then
<span style="color:blue Me.SelectionLength = <span style="color:blue Me.Text.Length - tSelectionStart
<span style="color:blue Me.SelectionColor = Color.Red
redColoring = <span style="color:blue True
<span style="color:blue Else
redColoring = <span style="color:blue False
<span style="color:blue Me.SelectionLength = quote.Index - Quotes(QuoteCounter - 1).Index
<span style="color:blue Me.SelectionColor = Color.Red
<span style="color:blue End <span style="color:blue If
<span style="color:blue Else
<span style="color:blue Dim tlen <span style="color:blue As <span style="color:blue Integer = Quotes(QuoteCounter - 2).Index
<span style="color:blue Me.SelectionLength = quote.Index - tlen + 1
<span style="color:blue Me.SelectionColor = Color.Red
redColoring = <span style="color:blue True
<span style="color:blue End <span style="color:blue If
<span style="color:blue Next
<span style="color:blue End <span style="color:blue If
<span style="color:green color double quotes and brackets
<span style="color:blue Dim dblQuotes <span style="color:blue As MatchCollection = Regex.Matches(rgl, <span style="color:#a31515 "(?<q>[""]|[()]|[[]]|[-*+$/.,])", RegexOptions.None)
<span style="color:blue For <span style="color:blue Each dblQuote <span style="color:blue As Match <span style="color:blue In dblQuotes
<span style="color:blue Me.SelectionStart = dblQuote.Index
<span style="color:blue Me.SelectionLength = 1
<span style="color:blue Me.SelectionColor = Color.FromArgb(255, 163, 21, 21)
<span style="color:blue Next
<span style="color:green check for comments using regex
<span style="color:blue Dim comments <span style="color:blue As MatchCollection
tSelectionStart = <span style="color:blue Nothing
tSelectionLength = <span style="color:blue Nothing
comments = Regex.Matches(rgl, <span style="color:#a31515 "(?<comments>--)", RegexOptions.None)
<span style="color:green If no comments were used, this code will not fire
<span style="color:green and therefore wont un-necessarily impact the performance
<span style="color:blue If comments.<span style="color:blue Count > 0 <span style="color:blue Then
<span style="color:blue For <span style="color:blue Each c <span style="color:blue As Match <span style="color:blue In comments
tSelectionStart = c.Index
tLineIndex = <span style="color:blue Me.GetLineFromCharIndex(tSelectionStart)
tIndexFirstCharOnLine = <span style="color:blue Me.GetFirstCharIndexFromLine(tLineIndex)
tLineLength = <span style="color:blue Me.Lines(tLineIndex).Length
<span style="color:blue Me.SelectionStart = tSelectionStart
tSelectionLength = tLineLength - (tSelectionStart - tIndexFirstCharOnLine)
<span style="color:blue Me.SelectionLength = tSelectionLength
<span style="color:blue Me.SelectionColor = Color.Green
<span style="color:blue Next
<span style="color:blue End <span style="color:blue If
<span style="color:green Restore the selectionstart
<span style="color:blue Me.SelectionStart = SelectionAt
<span style="color:blue Me.SelectionLength = 0
DefaultColor = Color.Black
<span style="color:blue Me.SelectionColor = Color.Black
LockWindowUpdate(0)
<span style="color:blue End <span style="color:blue If
<span style="color:blue Catch ex <span style="color:blue As Exception
<span style="color:blue End <span style="color:blue Try
<span style="color:blue End <span style="color:blue Sub
<span style="color:blue Public <span style="color:blue Function GetCharFromLineIndex(<span style="color:blue ByVal LineIndex <span style="color:blue As <span style="color:blue Integer) <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Return SendMessage(<span style="color:blue Me.Handle, EditMessages.LineIndex, LineIndex, 0)
<span style="color:blue End <span style="color:blue Function
<span style="color:blue Public <span style="color:blue Function FirstVisibleLine() <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Return SendMessage(<span style="color:blue Me.Handle, EditMessages.GetFirstVisibleLine, 0, 0)
<span style="color:blue End <span style="color:blue Function
<span style="color:blue Public <span style="color:blue Function LastVisibleLine() <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Dim LastLine <span style="color:blue As <span style="color:blue Integer = FirstVisibleLine() + (<span style="color:blue Me.Height / <span style="color:blue Me.Font.Height)
<span style="color:blue If LastLine > <span style="color:blue Me.Lines.Length <span style="color:blue Or LastLine = 0 <span style="color:blue Then
LastLine = <span style="color:blue Me.Lines.Length
<span style="color:blue End <span style="color:blue If
<span style="color:blue Return LastLine
<span style="color:blue End <span style="color:blue Function
<span style="color:blue Public <span style="color:blue Sub <span style="color:blue New()
<span style="color:blue Dim MyRow <span style="color:blue As DataRow
<span style="color:blue Dim arrKeyWords() <span style="color:blue As <span style="color:blue String, strKW <span style="color:blue As <span style="color:blue String
<span style="color:blue Me.AcceptsTab = <span style="color:blue True
<span style="color:blue Me.Font = <span style="color:blue New Font(<span style="color:#a31515 "Courier New", 12, FontStyle.Regular)
<span style="color:blue Me.WordWrap = <span style="color:blue False
<span style="color:green Load all the keywords and the colors to make them
Words.Columns.Add(<span style="color:#a31515 "Word")
Words.PrimaryKey = <span style="color:blue New DataColumn() {Words.Columns(0)}
Words.Columns.Add(<span style="color:#a31515 "Color")
<span style="color:green blue keywords
arrKeyWords = <span style="color:blue New <span style="color:blue String() {<span style="color:#a31515 "absolute", _
<span style="color:#a31515 "action", <span style="color:#a31515 "add", <span style="color:#a31515 "alter", <span style="color:#a31515 "asc", <span style="color:#a31515 "as", <span style="color:#a31515 "authorization", <span style="color:#a31515 "begin", <span style="color:#a31515 "bit", _
<span style="color:#a31515 "bit_length", <span style="color:#a31515 "by", <span style="color:#a31515 "cascade", <span style="color:#a31515 "case", <span style="color:#a31515 "catalog", <span style="color:#a31515 "char", <span style="color:#a31515 "character", _
<span style="color:#a31515 "check", <span style="color:#a31515 "close", <span style="color:#a31515 "collate", <span style="color:#a31515 "column", <span style="color:#a31515 "commit", <span style="color:#a31515 "connect", <span style="color:#a31515 "constraint", _
<span style="color:#a31515 "continue", <span style="color:#a31515 "create", <span style="color:#a31515 "current", <span style="color:#a31515 "current_date", <span style="color:#a31515 "current_time", <span style="color:#a31515 "cursor", _
<span style="color:#a31515 "date", <span style="color:#a31515 "deallocate", <span style="color:#a31515 "dec", <span style="color:#a31515 "decimal", <span style="color:#a31515 "declare", <span style="color:#a31515 "default", <span style="color:#a31515 "delete", _
<span style="color:#a31515 "desc", <span style="color:#a31515 "distinct", <span style="color:#a31515 "double", <span style="color:#a31515 "drop", <span style="color:#a31515 "else", <span style="color:#a31515 "end", <span style="color:#a31515 "end-exec", <span style="color:#a31515 "escape", _
<span style="color:#a31515 "except", <span style="color:#a31515 "exec", <span style="color:#a31515 "execute", <span style="color:#a31515 "external", <span style="color:#a31515 "fetch", <span style="color:#a31515 "first", <span style="color:#a31515 "float", <span style="color:#a31515 "for", <span style="color:#a31515 "foreign", _
<span style="color:#a31515 "from", <span style="color:#a31515 "full", <span style="color:#a31515 "get", <span style="color:#a31515 "global", <span style="color:#a31515 "go", <span style="color:#a31515 "goto", <span style="color:#a31515 "grant", <span style="color:#a31515 "group", <span style="color:#a31515 "having", <span style="color:#a31515 "hour", _
<span style="color:#a31515 "identity", <span style="color:#a31515 "if", <span style="color:#a31515 "immediate", <span style="color:#a31515 "index", <span style="color:#a31515 "insensitive", <span style="color:#a31515 "insert", <span style="color:#a31515 "int", <span style="color:#a31515 "integer", <span style="color:#a31515 "intersect", _
<span style="color:#a31515 "into", <span style="color:#a31515 "isolation", <span style="color:#a31515 "key", <span style="color:#a31515 "language", <span style="color:#a31515 "last", <span style="color:#a31515 "level", <span style="color:#a31515 "local", <span style="color:#a31515 "minute", <span style="color:#a31515 "national", _
<span style="color:#a31515 "nchar", <span style="color:#a31515 "next", <span style="color:#a31515 "no", <span style="color:#a31515 "none", <span style="color:#a31515 "numeric", <span style="color:#a31515 "octet_length", <span style="color:#a31515 "of", <span style="color:#a31515 "on", <span style="color:#a31515 "open", <span style="color:#a31515 "option", _
<span style="color:#a31515 "order", <span style="color:#a31515 "output", <span style="color:#a31515 "partial", <span style="color:#a31515 "precision", <span style="color:#a31515 "primary", <span style="color:#a31515 "prior as", <span style="color:#a31515 "procedure", <span style="color:#a31515 "public", _
<span style="color:#a31515 "read", <span style="color:#a31515 "real", <span style="color:#a31515 "references", <span style="color:#a31515 "relative", <span style="color:#a31515 "restrict", <span style="color:#a31515 "revoke", <span style="color:#a31515 "rollback", <span style="color:#a31515 "rows", _
<span style="color:#a31515 "schema", <span style="color:#a31515 "scroll", <span style="color:#a31515 "second", <span style="color:#a31515 "select", <span style="color:#a31515 "session", <span style="color:#a31515 "set", <span style="color:#a31515 "smallint", <span style="color:#a31515 "sql", <span style="color:#a31515 "table", _
<span style="color:#a31515 "then", <span style="color:#a31515 "time", <span style="color:#a31515 "timestamp", <span style="color:#a31515 "to", <span style="color:#a31515 "transaction", <span style="color:#a31515 "union", <span style="color:#a31515 "unique", <span style="color:#a31515 "update", <span style="color:#a31515 "using", _
<span style="color:#a31515 "values", <span style="color:#a31515 "varchar", <span style="color:#a31515 "varying", <span style="color:#a31515 "view", <span style="color:#a31515 "when", <span style="color:#a31515 "where", <span style="color:#a31515 "with"}
<span style="color:blue For <span style="color:blue Each strKW <span style="color:blue In arrKeyWords
MyRow = Words.NewRow()
MyRow(<span style="color:#a31515 "Word") = strKW
MyRow(<span style="color:#a31515 "Color") = Color.Blue.Name
Words.Rows.Add(MyRow)
<span style="color:blue Next
<span style="color:green grey keywords
arrKeyWords = <span style="color:blue New <span style="color:blue String() {<span style="color:#a31515 "all", <span style="color:#a31515 "and", <span style="color:#a31515 "exists", <span style="color:#a31515 "any", _
<span style="color:#a31515 "between", <span style="color:#a31515 "in", <span style="color:#a31515 "inner", <span style="color:#a31515 "is", _
<span style="color:#a31515 "some", <span style="color:#a31515 "join", <span style="color:#a31515 "cross", <span style="color:#a31515 "left", _
<span style="color:#a31515 "right", <span style="color:#a31515 "like", <span style="color:#a31515 "not", <span style="color:#a31515 "null", _
<span style="color:#a31515 "or", <span style="color:#a31515 "outer", <span style="color:#a31515 "true", <span style="color:#a31515 "false"}
<span style="color:blue For <span style="color:blue Each strKW <span style="color:blue In arrKeyWords
MyRow = Words.NewRow()
MyRow(<span style="color:#a31515 "Word") = strKW
MyRow(<span style="color:#a31515 "Color") = <span style="color:#a31515 "Gray"
Words.Rows.Add(MyRow)
<span style="color:blue Next
arrKeyWords = <span style="color:blue New <span style="color:blue String() {<span style="color:#a31515 "isnull", <span style="color:#a31515 "ascii", <span style="color:#a31515 "asci", <span style="color:#a31515 "upper", <span style="color:#a31515 "user", <span style="color:#a31515 "year", _
<span style="color:#a31515 "avg", <span style="color:#a31515 "cast", <span style="color:#a31515 "coalesce", <span style="color:#a31515 "convert", <span style="color:#a31515 "count", <span style="color:#a31515 "current_timestamp", _
<span style="color:#a31515 "current_user", <span style="color:#a31515 "datediff", <span style="color:#a31515 "day", <span style="color:#a31515 "grouping", <span style="color:#a31515 "len", <span style="color:#a31515 "lower", <span style="color:#a31515 "max", <span style="color:#a31515 "min", <span style="color:#a31515 "month", _
<span style="color:#a31515 "nullif", <span style="color:#a31515 "session_user", <span style="color:#a31515 "space", <span style="color:#a31515 "substring", <span style="color:#a31515 "sum", <span style="color:#a31515 "system_user"}
<span style="color:blue For <span style="color:blue Each strKW <span style="color:blue In arrKeyWords
MyRow = Words.NewRow()
MyRow(<span style="color:#a31515 "Word") = strKW
MyRow(<span style="color:#a31515 "Color") = <span style="color:#a31515 "Chocolate"
Words.Rows.Add(MyRow)
<span style="color:blue Next
<span style="color:green Green keywords
arrKeyWords = <span style="color:blue New <span style="color:blue String() {<span style="color:#a31515 "sysaltfiles", <span style="color:#a31515 "syscacheobjects", <span style="color:#a31515 "syscharsets", <span style="color:#a31515 "syscolumns", _
<span style="color:#a31515 "syscomments", <span style="color:#a31515 "sysconfigures", <span style="color:#a31515 "sysconstraints", <span style="color:#a31515 "syscurconfigs", _
<span style="color:#a31515 "sysdatabases", <span style="color:#a31515 "sysdepends", <span style="color:#a31515 "sysdevices", <span style="color:#a31515 "sysfilegroups", _
<span style="color:#a31515 "sysfiles", <span style="color:#a31515 "sysforeignkeys", <span style="color:#a31515 "sysfulltextcatalogs", <span style="color:#a31515 "sysindexes", _
<span style="color:#a31515 "sysindexkeys", <span style="color:#a31515 "syslanguages", <span style="color:#a31515 "syslockinfo", <span style="color:#a31515 "syslogins", _
<span style="color:#a31515 "sysmembers", <span style="color:#a31515 "sysmessages", <span style="color:#a31515 "sysobjects", <span style="color:#a31515 "sysoledbusers", _
<span style="color:#a31515 "sysperfinfo", <span style="color:#a31515 "syspermissions", <span style="color:#a31515 "sysprocesses", <span style="color:#a31515 "sysprotects", _
<span style="color:#a31515 "sysreferences", <span style="color:#a31515 "sysremotelogins", <span style="color:#a31515 "sysservers", <span style="color:#a31515 "systypes", <span style="color:#a31515 "sysusers"}
<span style="color:blue For <span style="color:blue Each strKW <span style="color:blue In arrKeyWords
MyRow = Words.NewRow()
MyRow(<span style="color:#a31515 "Word") = strKW
MyRow(<span style="color:#a31515 "Color") = <span style="color:#a31515 "Green"
Words.Rows.Add(MyRow)
<span style="color:blue Next
<span style="color:blue End <span style="color:blue Sub
<span style="color:blue End <span style="color:blue Class
<br/>[/code]
Can you guys help me in stopping this unwanted text jumping?
thanks!
Cheers,
John
<br/>
View the full article
Im building an app that has an sql editor with syntax highlighting and intellisense. Ive got the frame, but im stuck with the re-coloring of the RTB. - NOT performance wise.... got that covered... but with the focus inside the RTB.
Suppose the RTB has got a large volume of lines. when i start editing, the on-textchange event triggers the re-coloring of the current line. using;
- selectionStart
- selectionLength
- selectionColor
I am locking the gui, and i am resetting the focus properly. BUT,.. here it comes, ... when im editing at the bottom of the text file, the text in the RTB jumps up...
Picture 1, initial state begin
<img src="http://i978.photobucket.com/albums/ae264/jb_theman/Clipboard02-4.png" border="0" alt="
Picture 2, start editing on line 5
<img src="http://i978.photobucket.com/albums/ae264/jb_theman/Clipboard04-1.png" border="0" alt="
Picture 3, text jumps up, focus is on the right line, but now it is the first one that is displayed.
<img src="http://i978.photobucket.com/albums/ae264/jb_theman/Clipboard05-1.png" border="0" alt="
Code
<div style="color:black; background-color:white
<pre><span style="color:blue Imports System.Text.RegularExpressions
<span style="color:blue Public <span style="color:blue Class SQLRTB
<span style="color:blue Inherits System.Windows.Forms.RichTextBox
<span style="color:blue Public DefaultColor <span style="color:blue As Color
<span style="color:blue Private pDt <span style="color:blue As DataTable
<span style="color:blue Private pCnStr <span style="color:blue As <span style="color:blue String
<span style="color:blue Private cn <span style="color:blue As OleDb.OleDbConnection
<span style="color:blue Private Words <span style="color:blue As <span style="color:blue New DataTable
<span style="color:blue Private <span style="color:blue Declare <span style="color:blue Function SendMessage <span style="color:blue Lib <span style="color:#a31515 "user32" <span style="color:blue Alias <span style="color:#a31515 "SendMessageA" (<span style="color:blue ByVal hWnd <span style="color:blue As IntPtr, <span style="color:blue ByVal wMsg <span style="color:blue As <span style="color:blue Integer, <span style="color:blue ByVal wParam <span style="color:blue As <span style="color:blue Integer, <span style="color:blue ByVal lParam <span style="color:blue As <span style="color:blue Integer) <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Private <span style="color:blue Declare <span style="color:blue Function LockWindowUpdate <span style="color:blue Lib <span style="color:#a31515 "user32" (<span style="color:blue ByVal hWnd <span style="color:blue As <span style="color:blue Integer) <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Private LastChars(2) <span style="color:blue As <span style="color:blue Integer
<span style="color:green <summary>
<span style="color:green Get or sets the connectionstring to the database
<span style="color:green Used for:
<span style="color:green - Intellisense
<span style="color:green - Status updates
<span style="color:green </summary>
<span style="color:green <value></value>
<span style="color:green <returns>Connectionstring to the database</returns>
<span style="color:green <remarks></remarks>
<span style="color:blue Public <span style="color:blue Property ConnectionString() <span style="color:blue As <span style="color:blue String
<span style="color:blue Get
<span style="color:blue Return pCnStr
<span style="color:blue End <span style="color:blue Get
<span style="color:blue Set(<span style="color:blue ByVal value <span style="color:blue As <span style="color:blue String)
<span style="color:blue Try
<span style="color:blue Using cn <span style="color:blue As <span style="color:blue New OleDb.OleDbConnection(value)
cn.Open()
pCnStr = value
fillIntellisensetable()
<span style="color:blue End <span style="color:blue Using
<span style="color:blue Catch ex <span style="color:blue As Exception
cn = <span style="color:blue Nothing
<span style="color:blue End <span style="color:blue Try
<span style="color:blue End <span style="color:blue Set
<span style="color:blue End <span style="color:blue Property
<span style="color:green Keep polling the entered characters to look for an intelli-event
<span style="color:blue Protected <span style="color:blue Overrides <span style="color:blue Sub OnKeyDown(<span style="color:blue ByVal e <span style="color:blue As System.Windows.Forms.KeyEventArgs)
<span style="color:blue MyBase.OnKeyDown(e)
<span style="color:green a = 65
<span style="color:green z = 90
<span style="color:green 0 = 96
<span style="color:green 9 = 105
<span style="color:green 32 = space
<span style="color:green [65 - 90] [96 - 105]
<span style="color:green if it was a "." (dot) try to find a table that matches the previous word
<span style="color:green check pressed key for its range
<span style="color:blue If (e.KeyValue >= 65 <span style="color:blue And e.KeyValue <= 90) <span style="color:blue Or (e.KeyValue >= 96 <span style="color:blue And e.KeyValue <= 105) <span style="color:blue Or e.KeyValue = 32 <span style="color:blue Then
LastChars(0) = LastChars(1)
LastChars(1) = LastChars(2)
LastChars(2) = e.KeyData
<span style="color:blue If LastChars(0) = 32 <span style="color:blue And LastChars(1) <> 32 <span style="color:blue And LastChars(2) <> 32 <span style="color:blue Then
<span style="color:green if the user typed a space and two characters in one go, look for objects
<span style="color:green that start with the last two letters
<span style="color:green set location of intellisense window
<span style="color:blue Dim pnt <span style="color:blue As <span style="color:blue New System.Drawing.Point
<span style="color:blue With pnt
.X = <span style="color:blue Me.PointToScreen(<span style="color:blue Me.GetPositionFromCharIndex(<span style="color:blue Me.SelectionStart())).X
.Y = <span style="color:blue Me.PointToScreen(<span style="color:blue Me.GetPositionFromCharIndex(<span style="color:blue Me.SelectionStart())).Y + <span style="color:blue Me.FontHeight
<span style="color:blue End <span style="color:blue With
<span style="color:blue Dim intll <span style="color:blue As <span style="color:blue New IntelliSense(pDt, Chr(LastChars(1)) & Chr(LastChars(2)), <span style="color:blue Me, pnt)
<span style="color:blue End <span style="color:blue If
<span style="color:blue Else
LastChars(0) = 0
LastChars(1) = 0
LastChars(2) = 0
<span style="color:blue End <span style="color:blue If
<span style="color:blue End <span style="color:blue Sub
<span style="color:green control + v fires a -complete- re-coloring
<span style="color:blue Protected <span style="color:blue Overrides <span style="color:blue Function ProcessCmdKey(<span style="color:blue ByRef m <span style="color:blue As System.Windows.Forms.Message, <span style="color:blue ByVal keyData <span style="color:blue As System.Windows.Forms.Keys) <span style="color:blue As <span style="color:blue Boolean
<span style="color:green Use this to catch paste events, if text was pasted, refresh the whole RTB
<span style="color:green otherwise the update for the current line is good enough
<span style="color:blue Dim KeyDown <span style="color:blue As <span style="color:blue Integer = &H100
<span style="color:blue Dim SystemKeyDown <span style="color:blue As <span style="color:blue Integer = &H104
<span style="color:blue If m.Msg = KeyDown <span style="color:blue Or m.Msg = SystemKeyDown <span style="color:blue Then
<span style="color:blue Select <span style="color:blue Case keyData
<span style="color:blue Case Keys.Control <span style="color:blue Or Keys.V
<span style="color:blue Me.Paste()
RefreshColors()
<span style="color:green If Clipboard.GetDataObject().GetDataPresent(DataFormats.Text, True) Then
<span style="color:green Me.Paste()
<span style="color:green ColorVisibleLines()
<span style="color:green ColorSpecialElements()
<span style="color:green End If
<span style="color:blue End <span style="color:blue Select
<span style="color:blue End <span style="color:blue If
<span style="color:blue End <span style="color:blue Function
<span style="color:green Contains Windows Messages for the SendMessage API call
<span style="color:blue Private <span style="color:blue Enum EditMessages
LineIndex = 187
LineFromChar = 201
GetFirstVisibleLine = 206
CharFromPos = 215
PosFromChar = 1062
<span style="color:blue End <span style="color:blue Enum
<span style="color:blue Public <span style="color:blue Enum IntelliSenseIcons
IDatabase = 0
ITable = 1
IColumn = 2
<span style="color:blue End <span style="color:blue Enum
<span style="color:green This fires the coloring while typing
<span style="color:blue Protected <span style="color:blue Overrides <span style="color:blue Sub OnTextChanged(<span style="color:blue ByVal e <span style="color:blue As System.EventArgs)
ColorLineNumber(<span style="color:blue Me.GetLineFromCharIndex(<span style="color:blue Me.SelectionStart()), <span style="color:blue Me.GetFirstCharIndexFromLine(<span style="color:blue Me.GetLineFromCharIndex(<span style="color:blue Me.SelectionStart())))
ColorSpecialElements()
<span style="color:blue End <span style="color:blue Sub
<span style="color:green repaints all and colors
<span style="color:blue Public <span style="color:blue Sub RefreshColors()
<span style="color:blue Try
<span style="color:blue Dim lncnt <span style="color:blue As <span style="color:blue Integer = 0
<span style="color:blue For <span style="color:blue Each ln <span style="color:blue In <span style="color:blue Me.Lines
ColorLineNumber(lncnt, GetCharFromLineIndex(lncnt))
lncnt += 1
<span style="color:blue Next
ColorSpecialElements()
<span style="color:blue Catch ex <span style="color:blue As Exception
Debug.Print(ex.ToString)
<span style="color:blue End <span style="color:blue Try
<span style="color:blue End <span style="color:blue Sub
<span style="color:blue Public <span style="color:blue ReadOnly <span style="color:blue Property GetLineIndexCurrentEdit() <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Get
<span style="color:blue Return <span style="color:blue Me.GetLineFromCharIndex(<span style="color:blue Me.SelectionStart())
<span style="color:blue End <span style="color:blue Get
<span style="color:blue End <span style="color:blue Property
<span style="color:blue Public <span style="color:blue ReadOnly <span style="color:blue Property GetCharIndexCurrentEdit() <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Get
<span style="color:blue Me.GetFirstCharIndexFromLine(<span style="color:blue Me.GetLineFromCharIndex(<span style="color:blue Me.SelectionStart()))
<span style="color:blue End <span style="color:blue Get
<span style="color:blue End <span style="color:blue Property
<span style="color:green <summary>
<span style="color:green Re-paints all visible lines
<span style="color:green </summary>
<span style="color:green <remarks></remarks>
<span style="color:blue Public <span style="color:blue Sub ColorVisibleLines()
<span style="color:blue Dim FirstLine <span style="color:blue As <span style="color:blue Integer = FirstVisibleLine()
<span style="color:blue Dim LastLine <span style="color:blue As <span style="color:blue Integer = LastVisibleLine()
<span style="color:blue Dim FirstVisibleChar <span style="color:blue As <span style="color:blue Integer
<span style="color:blue If (FirstLine = 0) <span style="color:blue And (LastLine = 0) <span style="color:blue Then
<span style="color:green If there is no text it will error, so exit the sub
<span style="color:blue Exit <span style="color:blue Sub
<span style="color:blue Else
<span style="color:blue While FirstLine < LastLine
FirstVisibleChar = GetCharFromLineIndex(FirstLine)
ColorLineNumber(FirstLine, FirstVisibleChar)
FirstLine += 1
<span style="color:blue End <span style="color:blue While
<span style="color:blue End <span style="color:blue If
<span style="color:blue End <span style="color:blue Sub
<span style="color:blue Public <span style="color:blue Sub ColorLineNumber(<span style="color:blue ByVal LineIndex <span style="color:blue As <span style="color:blue Integer, <span style="color:blue ByVal lStart <span style="color:blue As <span style="color:blue Integer)
<span style="color:blue Try
<span style="color:blue If FirstVisibleLine() = 0 <span style="color:blue And LastVisibleLine() = 0 <span style="color:blue Then
<span style="color:blue Exit <span style="color:blue Sub
<span style="color:blue Else
<span style="color:blue Dim i <span style="color:blue As <span style="color:blue Integer = 0
<span style="color:blue Dim SelectionAt <span style="color:blue As <span style="color:blue Integer = <span style="color:blue Me.SelectionStart
<span style="color:blue Dim MyRow <span style="color:blue As DataRow
<span style="color:blue Dim Line() <span style="color:blue As <span style="color:blue String, MyI <span style="color:blue As <span style="color:blue Integer, MyStr <span style="color:blue As <span style="color:blue String
DefaultColor = Color.Black
<span style="color:green Lock the update
LockWindowUpdate(<span style="color:blue Me.Handle.ToInt32)
MyI = lStart
Line = Split(IgnoreControlCharacters(<span style="color:blue Me.Lines(LineIndex).ToString), <span style="color:#a31515 " ")
<span style="color:green Do keywords
<span style="color:blue For <span style="color:blue Each MyStr <span style="color:blue In Line
<span style="color:blue Me.SelectionStart = MyI
<span style="color:blue Me.SelectionLength = MyStr.Length
<span style="color:blue If Words.Rows.Contains(MyStr) <span style="color:blue Then
MyRow = Words.Rows.Find(MyStr)
<span style="color:blue Me.SelectionColor = Color.FromName(MyRow(<span style="color:#a31515 "Color"))
<span style="color:blue Else
<span style="color:blue Me.SelectionColor = DefaultColor
<span style="color:blue If Mid(MyStr, 1, 1) = <span style="color:#a31515 "@" <span style="color:blue Then
<span style="color:blue Me.SelectionColor = Color.Magenta
<span style="color:blue End <span style="color:blue If
<span style="color:blue End <span style="color:blue If
MyI += MyStr.Length + 1
<span style="color:blue Next
<span style="color:green Restore the selectionstart
<span style="color:blue Me.SelectionStart = SelectionAt
<span style="color:blue Me.SelectionLength = 0
DefaultColor = Color.Black
<span style="color:blue Me.SelectionColor = Color.Black
LockWindowUpdate(0)
<span style="color:blue End <span style="color:blue If
<span style="color:blue Catch ex <span style="color:blue As Exception
<span style="color:green Unlock the update
LockWindowUpdate(0)
<span style="color:blue End <span style="color:blue Try
<span style="color:blue End <span style="color:blue Sub
<span style="color:blue Public <span style="color:blue Sub ColorSpecialElements()
<span style="color:blue Try
<span style="color:blue If FirstVisibleLine() = 0 <span style="color:blue And LastVisibleLine() = 0 <span style="color:blue Then
<span style="color:blue Exit <span style="color:blue Sub
<span style="color:blue Else
LockWindowUpdate(<span style="color:blue Me.Handle.ToInt32)
<span style="color:blue Dim i <span style="color:blue As <span style="color:blue Integer = 0
<span style="color:blue Dim SelectionAt <span style="color:blue As <span style="color:blue Integer = <span style="color:blue Me.SelectionStart
<span style="color:blue Dim tSelectionStart <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Dim tSelectionLength <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Dim tLineIndex <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Dim tIndexFirstCharOnLine <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Dim tLineLength <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Dim rgl <span style="color:blue As <span style="color:blue String = <span style="color:blue Me.Text
<span style="color:green check for strings using regex
<span style="color:blue Dim Quotes <span style="color:blue As MatchCollection
<span style="color:blue Dim redColoring <span style="color:blue As <span style="color:blue Boolean = <span style="color:blue True
<span style="color:green re-using variables can cause hours of debugging -trust me-
tSelectionStart = <span style="color:blue Nothing
tSelectionLength = <span style="color:blue Nothing
<span style="color:blue Dim QuoteCounter <span style="color:blue As <span style="color:blue Integer = 0
Quotes = Regex.Matches(rgl, <span style="color:#a31515 "(?<q>)", RegexOptions.None)
<span style="color:green if there arent any string quantifiers, dont impact the performance
<span style="color:blue If Quotes.<span style="color:blue Count > 0 <span style="color:blue Then
<span style="color:blue For <span style="color:blue Each quote <span style="color:blue As Match <span style="color:blue In Quotes
QuoteCounter += 1
<span style="color:blue If redColoring = <span style="color:blue True <span style="color:blue Then
tSelectionStart = quote.Index
<span style="color:blue Me.SelectionStart = tSelectionStart
<span style="color:blue If QuoteCounter = Quotes.<span style="color:blue Count <span style="color:blue Then
<span style="color:blue Me.SelectionLength = <span style="color:blue Me.Text.Length - tSelectionStart
<span style="color:blue Me.SelectionColor = Color.Red
redColoring = <span style="color:blue True
<span style="color:blue Else
redColoring = <span style="color:blue False
<span style="color:blue Me.SelectionLength = quote.Index - Quotes(QuoteCounter - 1).Index
<span style="color:blue Me.SelectionColor = Color.Red
<span style="color:blue End <span style="color:blue If
<span style="color:blue Else
<span style="color:blue Dim tlen <span style="color:blue As <span style="color:blue Integer = Quotes(QuoteCounter - 2).Index
<span style="color:blue Me.SelectionLength = quote.Index - tlen + 1
<span style="color:blue Me.SelectionColor = Color.Red
redColoring = <span style="color:blue True
<span style="color:blue End <span style="color:blue If
<span style="color:blue Next
<span style="color:blue End <span style="color:blue If
<span style="color:green color double quotes and brackets
<span style="color:blue Dim dblQuotes <span style="color:blue As MatchCollection = Regex.Matches(rgl, <span style="color:#a31515 "(?<q>[""]|[()]|[[]]|[-*+$/.,])", RegexOptions.None)
<span style="color:blue For <span style="color:blue Each dblQuote <span style="color:blue As Match <span style="color:blue In dblQuotes
<span style="color:blue Me.SelectionStart = dblQuote.Index
<span style="color:blue Me.SelectionLength = 1
<span style="color:blue Me.SelectionColor = Color.FromArgb(255, 163, 21, 21)
<span style="color:blue Next
<span style="color:green check for comments using regex
<span style="color:blue Dim comments <span style="color:blue As MatchCollection
tSelectionStart = <span style="color:blue Nothing
tSelectionLength = <span style="color:blue Nothing
comments = Regex.Matches(rgl, <span style="color:#a31515 "(?<comments>--)", RegexOptions.None)
<span style="color:green If no comments were used, this code will not fire
<span style="color:green and therefore wont un-necessarily impact the performance
<span style="color:blue If comments.<span style="color:blue Count > 0 <span style="color:blue Then
<span style="color:blue For <span style="color:blue Each c <span style="color:blue As Match <span style="color:blue In comments
tSelectionStart = c.Index
tLineIndex = <span style="color:blue Me.GetLineFromCharIndex(tSelectionStart)
tIndexFirstCharOnLine = <span style="color:blue Me.GetFirstCharIndexFromLine(tLineIndex)
tLineLength = <span style="color:blue Me.Lines(tLineIndex).Length
<span style="color:blue Me.SelectionStart = tSelectionStart
tSelectionLength = tLineLength - (tSelectionStart - tIndexFirstCharOnLine)
<span style="color:blue Me.SelectionLength = tSelectionLength
<span style="color:blue Me.SelectionColor = Color.Green
<span style="color:blue Next
<span style="color:blue End <span style="color:blue If
<span style="color:green Restore the selectionstart
<span style="color:blue Me.SelectionStart = SelectionAt
<span style="color:blue Me.SelectionLength = 0
DefaultColor = Color.Black
<span style="color:blue Me.SelectionColor = Color.Black
LockWindowUpdate(0)
<span style="color:blue End <span style="color:blue If
<span style="color:blue Catch ex <span style="color:blue As Exception
<span style="color:blue End <span style="color:blue Try
<span style="color:blue End <span style="color:blue Sub
<span style="color:blue Public <span style="color:blue Function GetCharFromLineIndex(<span style="color:blue ByVal LineIndex <span style="color:blue As <span style="color:blue Integer) <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Return SendMessage(<span style="color:blue Me.Handle, EditMessages.LineIndex, LineIndex, 0)
<span style="color:blue End <span style="color:blue Function
<span style="color:blue Public <span style="color:blue Function FirstVisibleLine() <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Return SendMessage(<span style="color:blue Me.Handle, EditMessages.GetFirstVisibleLine, 0, 0)
<span style="color:blue End <span style="color:blue Function
<span style="color:blue Public <span style="color:blue Function LastVisibleLine() <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Dim LastLine <span style="color:blue As <span style="color:blue Integer = FirstVisibleLine() + (<span style="color:blue Me.Height / <span style="color:blue Me.Font.Height)
<span style="color:blue If LastLine > <span style="color:blue Me.Lines.Length <span style="color:blue Or LastLine = 0 <span style="color:blue Then
LastLine = <span style="color:blue Me.Lines.Length
<span style="color:blue End <span style="color:blue If
<span style="color:blue Return LastLine
<span style="color:blue End <span style="color:blue Function
<span style="color:blue Public <span style="color:blue Sub <span style="color:blue New()
<span style="color:blue Dim MyRow <span style="color:blue As DataRow
<span style="color:blue Dim arrKeyWords() <span style="color:blue As <span style="color:blue String, strKW <span style="color:blue As <span style="color:blue String
<span style="color:blue Me.AcceptsTab = <span style="color:blue True
<span style="color:blue Me.Font = <span style="color:blue New Font(<span style="color:#a31515 "Courier New", 12, FontStyle.Regular)
<span style="color:blue Me.WordWrap = <span style="color:blue False
<span style="color:green Load all the keywords and the colors to make them
Words.Columns.Add(<span style="color:#a31515 "Word")
Words.PrimaryKey = <span style="color:blue New DataColumn() {Words.Columns(0)}
Words.Columns.Add(<span style="color:#a31515 "Color")
<span style="color:green blue keywords
arrKeyWords = <span style="color:blue New <span style="color:blue String() {<span style="color:#a31515 "absolute", _
<span style="color:#a31515 "action", <span style="color:#a31515 "add", <span style="color:#a31515 "alter", <span style="color:#a31515 "asc", <span style="color:#a31515 "as", <span style="color:#a31515 "authorization", <span style="color:#a31515 "begin", <span style="color:#a31515 "bit", _
<span style="color:#a31515 "bit_length", <span style="color:#a31515 "by", <span style="color:#a31515 "cascade", <span style="color:#a31515 "case", <span style="color:#a31515 "catalog", <span style="color:#a31515 "char", <span style="color:#a31515 "character", _
<span style="color:#a31515 "check", <span style="color:#a31515 "close", <span style="color:#a31515 "collate", <span style="color:#a31515 "column", <span style="color:#a31515 "commit", <span style="color:#a31515 "connect", <span style="color:#a31515 "constraint", _
<span style="color:#a31515 "continue", <span style="color:#a31515 "create", <span style="color:#a31515 "current", <span style="color:#a31515 "current_date", <span style="color:#a31515 "current_time", <span style="color:#a31515 "cursor", _
<span style="color:#a31515 "date", <span style="color:#a31515 "deallocate", <span style="color:#a31515 "dec", <span style="color:#a31515 "decimal", <span style="color:#a31515 "declare", <span style="color:#a31515 "default", <span style="color:#a31515 "delete", _
<span style="color:#a31515 "desc", <span style="color:#a31515 "distinct", <span style="color:#a31515 "double", <span style="color:#a31515 "drop", <span style="color:#a31515 "else", <span style="color:#a31515 "end", <span style="color:#a31515 "end-exec", <span style="color:#a31515 "escape", _
<span style="color:#a31515 "except", <span style="color:#a31515 "exec", <span style="color:#a31515 "execute", <span style="color:#a31515 "external", <span style="color:#a31515 "fetch", <span style="color:#a31515 "first", <span style="color:#a31515 "float", <span style="color:#a31515 "for", <span style="color:#a31515 "foreign", _
<span style="color:#a31515 "from", <span style="color:#a31515 "full", <span style="color:#a31515 "get", <span style="color:#a31515 "global", <span style="color:#a31515 "go", <span style="color:#a31515 "goto", <span style="color:#a31515 "grant", <span style="color:#a31515 "group", <span style="color:#a31515 "having", <span style="color:#a31515 "hour", _
<span style="color:#a31515 "identity", <span style="color:#a31515 "if", <span style="color:#a31515 "immediate", <span style="color:#a31515 "index", <span style="color:#a31515 "insensitive", <span style="color:#a31515 "insert", <span style="color:#a31515 "int", <span style="color:#a31515 "integer", <span style="color:#a31515 "intersect", _
<span style="color:#a31515 "into", <span style="color:#a31515 "isolation", <span style="color:#a31515 "key", <span style="color:#a31515 "language", <span style="color:#a31515 "last", <span style="color:#a31515 "level", <span style="color:#a31515 "local", <span style="color:#a31515 "minute", <span style="color:#a31515 "national", _
<span style="color:#a31515 "nchar", <span style="color:#a31515 "next", <span style="color:#a31515 "no", <span style="color:#a31515 "none", <span style="color:#a31515 "numeric", <span style="color:#a31515 "octet_length", <span style="color:#a31515 "of", <span style="color:#a31515 "on", <span style="color:#a31515 "open", <span style="color:#a31515 "option", _
<span style="color:#a31515 "order", <span style="color:#a31515 "output", <span style="color:#a31515 "partial", <span style="color:#a31515 "precision", <span style="color:#a31515 "primary", <span style="color:#a31515 "prior as", <span style="color:#a31515 "procedure", <span style="color:#a31515 "public", _
<span style="color:#a31515 "read", <span style="color:#a31515 "real", <span style="color:#a31515 "references", <span style="color:#a31515 "relative", <span style="color:#a31515 "restrict", <span style="color:#a31515 "revoke", <span style="color:#a31515 "rollback", <span style="color:#a31515 "rows", _
<span style="color:#a31515 "schema", <span style="color:#a31515 "scroll", <span style="color:#a31515 "second", <span style="color:#a31515 "select", <span style="color:#a31515 "session", <span style="color:#a31515 "set", <span style="color:#a31515 "smallint", <span style="color:#a31515 "sql", <span style="color:#a31515 "table", _
<span style="color:#a31515 "then", <span style="color:#a31515 "time", <span style="color:#a31515 "timestamp", <span style="color:#a31515 "to", <span style="color:#a31515 "transaction", <span style="color:#a31515 "union", <span style="color:#a31515 "unique", <span style="color:#a31515 "update", <span style="color:#a31515 "using", _
<span style="color:#a31515 "values", <span style="color:#a31515 "varchar", <span style="color:#a31515 "varying", <span style="color:#a31515 "view", <span style="color:#a31515 "when", <span style="color:#a31515 "where", <span style="color:#a31515 "with"}
<span style="color:blue For <span style="color:blue Each strKW <span style="color:blue In arrKeyWords
MyRow = Words.NewRow()
MyRow(<span style="color:#a31515 "Word") = strKW
MyRow(<span style="color:#a31515 "Color") = Color.Blue.Name
Words.Rows.Add(MyRow)
<span style="color:blue Next
<span style="color:green grey keywords
arrKeyWords = <span style="color:blue New <span style="color:blue String() {<span style="color:#a31515 "all", <span style="color:#a31515 "and", <span style="color:#a31515 "exists", <span style="color:#a31515 "any", _
<span style="color:#a31515 "between", <span style="color:#a31515 "in", <span style="color:#a31515 "inner", <span style="color:#a31515 "is", _
<span style="color:#a31515 "some", <span style="color:#a31515 "join", <span style="color:#a31515 "cross", <span style="color:#a31515 "left", _
<span style="color:#a31515 "right", <span style="color:#a31515 "like", <span style="color:#a31515 "not", <span style="color:#a31515 "null", _
<span style="color:#a31515 "or", <span style="color:#a31515 "outer", <span style="color:#a31515 "true", <span style="color:#a31515 "false"}
<span style="color:blue For <span style="color:blue Each strKW <span style="color:blue In arrKeyWords
MyRow = Words.NewRow()
MyRow(<span style="color:#a31515 "Word") = strKW
MyRow(<span style="color:#a31515 "Color") = <span style="color:#a31515 "Gray"
Words.Rows.Add(MyRow)
<span style="color:blue Next
arrKeyWords = <span style="color:blue New <span style="color:blue String() {<span style="color:#a31515 "isnull", <span style="color:#a31515 "ascii", <span style="color:#a31515 "asci", <span style="color:#a31515 "upper", <span style="color:#a31515 "user", <span style="color:#a31515 "year", _
<span style="color:#a31515 "avg", <span style="color:#a31515 "cast", <span style="color:#a31515 "coalesce", <span style="color:#a31515 "convert", <span style="color:#a31515 "count", <span style="color:#a31515 "current_timestamp", _
<span style="color:#a31515 "current_user", <span style="color:#a31515 "datediff", <span style="color:#a31515 "day", <span style="color:#a31515 "grouping", <span style="color:#a31515 "len", <span style="color:#a31515 "lower", <span style="color:#a31515 "max", <span style="color:#a31515 "min", <span style="color:#a31515 "month", _
<span style="color:#a31515 "nullif", <span style="color:#a31515 "session_user", <span style="color:#a31515 "space", <span style="color:#a31515 "substring", <span style="color:#a31515 "sum", <span style="color:#a31515 "system_user"}
<span style="color:blue For <span style="color:blue Each strKW <span style="color:blue In arrKeyWords
MyRow = Words.NewRow()
MyRow(<span style="color:#a31515 "Word") = strKW
MyRow(<span style="color:#a31515 "Color") = <span style="color:#a31515 "Chocolate"
Words.Rows.Add(MyRow)
<span style="color:blue Next
<span style="color:green Green keywords
arrKeyWords = <span style="color:blue New <span style="color:blue String() {<span style="color:#a31515 "sysaltfiles", <span style="color:#a31515 "syscacheobjects", <span style="color:#a31515 "syscharsets", <span style="color:#a31515 "syscolumns", _
<span style="color:#a31515 "syscomments", <span style="color:#a31515 "sysconfigures", <span style="color:#a31515 "sysconstraints", <span style="color:#a31515 "syscurconfigs", _
<span style="color:#a31515 "sysdatabases", <span style="color:#a31515 "sysdepends", <span style="color:#a31515 "sysdevices", <span style="color:#a31515 "sysfilegroups", _
<span style="color:#a31515 "sysfiles", <span style="color:#a31515 "sysforeignkeys", <span style="color:#a31515 "sysfulltextcatalogs", <span style="color:#a31515 "sysindexes", _
<span style="color:#a31515 "sysindexkeys", <span style="color:#a31515 "syslanguages", <span style="color:#a31515 "syslockinfo", <span style="color:#a31515 "syslogins", _
<span style="color:#a31515 "sysmembers", <span style="color:#a31515 "sysmessages", <span style="color:#a31515 "sysobjects", <span style="color:#a31515 "sysoledbusers", _
<span style="color:#a31515 "sysperfinfo", <span style="color:#a31515 "syspermissions", <span style="color:#a31515 "sysprocesses", <span style="color:#a31515 "sysprotects", _
<span style="color:#a31515 "sysreferences", <span style="color:#a31515 "sysremotelogins", <span style="color:#a31515 "sysservers", <span style="color:#a31515 "systypes", <span style="color:#a31515 "sysusers"}
<span style="color:blue For <span style="color:blue Each strKW <span style="color:blue In arrKeyWords
MyRow = Words.NewRow()
MyRow(<span style="color:#a31515 "Word") = strKW
MyRow(<span style="color:#a31515 "Color") = <span style="color:#a31515 "Green"
Words.Rows.Add(MyRow)
<span style="color:blue Next
<span style="color:blue End <span style="color:blue Sub
<span style="color:blue End <span style="color:blue Class
<br/>[/code]
Can you guys help me in stopping this unwanted text jumping?
thanks!
Cheers,
John
<br/>
View the full article