Hey Everyone,
I am trying to find a way to highlight text in a rich text box. I have it the highlighting works but the richtextbox jumps around when you type on it. Basically, it moves the scroll bar one line per keypress until the text you are typing is at the top of the control.
I am using the following code to highlight
Functions of interest may be
FirstVisibleLine() gets the first line visible in the RichTextBox
LastVisibleLine() gets the Last Line visible
LockWindowUpdate() simply locks the UI while the coloring is happening.
Words is a dataset with two columns "word" and "color".
Word contains the regular expression and color is the color to highlight.
The basic purpose of this is to highlight a set of tokens that pretty much use the mask <*> or [*].
Does anyone have any suggestions as how to solve the jumping around problem?
Thanks in advance
I am trying to find a way to highlight text in a rich text box. I have it the highlighting works but the richtextbox jumps around when you type on it. Basically, it moves the scroll bar one line per keypress until the text you are typing is at the top of the control.
I am using the following code to highlight
Code:
Private Sub ColorVisibleLines()
Dim priorSelectStart = Me.RTFBox.SelectionStart
Dim priorSelectLength = Me.RTFBox.SelectionLength
Dim firstLine As Integer = FirstVisibleLine()
Dim lastLine As Integer = LastVisibleLine()
Dim currentLine As Integer = GetLineFromCharIndex(RTFBox.SelectionStart)
Dim currChar As Integer = GetCharFromLineIndex(currentLine)
' Lock the update
isLocked = True
LockWindowUpdate(RTFBox.Handle.ToInt32)
'make the current line black
Me.RTFBox.SelectionStart = currChar
Me.RTFBox.SelectionLength = Me.RTFBox.Lines(currentLine).Length
Me.RTFBox.SelectionColor = Color.Black
If firstLine = 0 And lastLine = 0 Then
Return
Else
Dim MyRow As DataRow
Dim rm As System.Text.RegularExpressions.MatchCollection
Dim m As System.Text.RegularExpressions.Match
For Each MyRow In Words.Rows
'"( |^)1.*2( |$)"
rm = System.Text.RegularExpressions.Regex.Matches(RTFBox.Text, MyRow("Word"))
For Each m In rm
Dim lineToColor = GetLineFromCharIndex(m.Index)
If lineToColor >= firstLine And lineToColor <= lastLine Then
RTFBox.SelectionStart = m.Index
RTFBox.SelectionLength = m.Length
RTFBox.SelectionColor = Color.FromName(MyRow("color"))
End If
Next
Next
End If
' Restore the selectionstart
Me.RTFBox.SelectionStart = priorSelectStart
Me.RTFBox.SelectionLength = 0
Me.RTFBox.SelectionColor = Color.Black
Me.RTFBox.SelectionLength = priorSelectLength
LockWindowUpdate(0)
isLocked = False
End Sub
Functions of interest may be
FirstVisibleLine() gets the first line visible in the RichTextBox
LastVisibleLine() gets the Last Line visible
LockWindowUpdate() simply locks the UI while the coloring is happening.
Code:
For Each MyRow In Words.Rows
Word contains the regular expression and color is the color to highlight.
The basic purpose of this is to highlight a set of tokens that pretty much use the mask <*> or [*].
Does anyone have any suggestions as how to solve the jumping around problem?
Thanks in advance