need with with small coding problem, re: richtextbox formatting

P

Paul M

hi there,

i have some code that is meant to search a richtextbox for a string and
hilight it in red, however, it only hilights the first instance of the word
found..

any help appreciated.

Dim intStartPosition As Integer = 1
Do While InStr(intStartPosition, rtf.Text.ToLower, value.Trim.ToLower) >
0
ntStartPosition = InStr(intStartPosition, rtf.Text.ToLower,
value.Trim.ToLower) + value.Trim.Length
rtf.SelectionStart = InStr(rtf.Text.ToLower, value.Trim.ToLower) - 1
rtf.SelectionLength = value.Trim.Length
rtf.SelectionColor = System.Drawing.Color.Red
If Not rtf.SelectionFont Is Nothing Then
Dim currentFont As System.Drawing.Font = rtf.SelectionFont
Dim newFontStyle As System.Drawing.FontStyle = FontStyle.Bold
rtf.SelectionFont = New Font(currentFont.FontFamily,
currentFont.Size, newFontStyle)
End If
Loop
 
P

Paul M

yes i have both option strict and explicit on.

rtf is a input parameter to a function that references a richtextbox.
It works fine and highlights the first found instance, but not the others..

any help appreciated,
Paul.
 
P

Paul M

thanks Bill,

that was a typo error while pasting the code into the message.

I cleaned it up little, but it still only hilights the first word found...

Dim intStartPosition As Integer = 1
Dim strValue As String = value.Trim.ToLower
Dim strRtf As String = rtf.Text.ToLower

Do While InStr(intStartPosition + 1, rtf.Text.ToLower, strValue) > 0
intStartPosition = InStr(intStartPosition, strRtf, strValue) +
strValue.Length
rtf.SelectionStart = InStr(strRtf, strValue) - 1
rtf.SelectionLength = strValue.Length
rtf.SelectionColor = System.Drawing.Color.Red
If Not rtf.SelectionFont Is Nothing Then
Dim currentFont As System.Drawing.Font = rtf.SelectionFont
Dim newFontStyle As System.Drawing.FontStyle =
FontStyle.Bold
rtf.SelectionFont = New Font(currentFont.FontFamily,
currentFont.Size, newFontStyle)
End If
Loop

thanks,
Paul.

Bill McCarthy said:
Right it works, but it definitely needs cleaning up. As to option explicit I
asked because the first line inside the loop reads as ntStartPosition, not
intStartPosition. That is the kind of error that option explicit would pick up
on.
 
B

Bill McCarthy

Hi Paul,

?Uhm, that code could do with a lot of cleaning up. I would suggest declaring a
local variable that replaces all the places you have "value.Trim" for instance.
Likewise for the rtf.Text.ToLower, assign that to a local variable.

Also, do you have Option Strict On and Option Explicit On ?


Bill.
 
B

Bill McCarthy

Hi Paul,

Right it works, but it definitely needs cleaning up. As to option explicit I
asked because the first line inside the loop reads as ntStartPosition, not
intStartPosition. That is the kind of error that option explicit would pick up
on.

So apart fro the above, I would suggest you change the instr loop to
intStartPosition + 1, otherwise you would keep finding the first position.
 
B

Bill McCarthy

Ah, just saw the problem, it is with your line that sets the selection start :
rtf.SelectionStart = InStr(rtf.Text.ToLower, value.Trim.ToLower) - 1

For reference, here is my version of the same function:


Function RTFHighlightWord(ByVal rtfbox As RichTextBox, ByVal word As String,
ByVal color As Drawing.Color, ByVal bold As Boolean) As Int32
Dim rtfText As String = rtfbox.Text.ToLower ' tolower for case
insensitivity

word = word.ToLower.Trim 'do we want to trim the word, or add " " to make
it exact Word ?
Dim wordLen As Int32 = word.Length

Dim pos As Int32 = rtfText.IndexOf(word)

Do While pos > 0
rtfbox.SelectionStart = pos
rtfbox.SelectionLength = wordLen
pos = rtfText.IndexOf(word, pos + wordLen)
rtfbox.SelectionColor = color
If bold Then rtfbox.SelectionFont = New Font(rtfbox.SelectionFont,
FontStyle.Bold)
Loop
End Function
 
B

Bill McCarthy

Hi Paul,

you're welcome. Note, there is a bug in my example though, it should have read
Do While Pos >= 0
;)

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top