Search and change color for multiple sub-strings in RichTextBox
Search and change color for multiple sub-strings in RichTextBox
Rate Search and change color for multiple sub-strings in RichTextBox
(1(1 Vote))
Dim lWhere, lPos As Long
Dim sTmp, sSearch As String
'set lPos to 1 for mid()
lPos = 1
'this is the searched string
sSearch = "DATA"
'loop the whole text
Do While lPos < Len(Me.RichTextBox1.Text)
'get sub string from the text
'this is because the InStr() returns the
'position of first occurence of the string...
sTmp = Mid(Me.RichTextBox1.Text, lPos, Len(Me.RichTextBox1.Text))
'find the string in sub string
lWhere = InStr(sTmp, sSearch)
'accumulate the lPos to be relative to the actual text
lPos = lPos + lWhere
If lWhere Then ' If found,
Me.RichTextBox1.SelStart = lPos - 2 ' set selection start and
Me.RichTextBox1.SelLength = Len(sSearch) ' set selection length. Else
Me.RichTextBox1.SelColor = RGB(255, 0, 0) 'change color to red
Else
Exit Do 'we are ready
End If
Loop
Search and change color for multiple sub-strings in RichTextBox Comments
No comments yet — be the first to post one!
Post a Comment