WINN2012
asked on
Change color of specific value in a richtexbox
I am attempting to use the following code to change the color of all the 1s in my field.
The field looks like this.
0010 1110 0000 1010 0001 1111 1000 0000 0110
The whole field ends up red instead of just the 1s.
Private Sub HighLightWords(ByVal Bitmap As String, ByVal myColor As Color)
''http://www.vbforums.com/showthread.php?125371-Change-Text-Color-and-Font-Character-by-Character
Dim strWord As String
Dim lPos As Long
strWord = "1"
If Bitmap = "Bitmap1" Then
lPos = InStr(1, Me.txtBinary1.Text, strWord, vbTextCompare)
If lPos > 0 Then
With txtBinary1
.SelectionStart = lPos - 1
.SelectionLength = Len(strWord)
.SelectionColor = myColor
.SelectionStart = Len(Me.txtBinary1.Text)
End With
End If
Else
lPos = InStr(1, Me.txtBinary2.Text, strWord, vbTextCompare)
If lPos > 0 Then
With txtBinary2
.SelectionStart = lPos - 1
.SelectionLength = Len(strWord)
.SelectionColor = myColor
.SelectionStart = Len(Me.txtBinary2.Text)
End With
End If
End If
End sub
The field looks like this.
0010 1110 0000 1010 0001 1111 1000 0000 0110
The whole field ends up red instead of just the 1s.
Private Sub HighLightWords(ByVal Bitmap As String, ByVal myColor As Color)
''http://www.vbforums.com/showthread.php?125371-Change-Text-Color-and-Font-Character-by-Character
Dim strWord As String
Dim lPos As Long
strWord = "1"
If Bitmap = "Bitmap1" Then
lPos = InStr(1, Me.txtBinary1.Text, strWord, vbTextCompare)
If lPos > 0 Then
With txtBinary1
.SelectionStart = lPos - 1
.SelectionLength = Len(strWord)
.SelectionColor = myColor
.SelectionStart = Len(Me.txtBinary1.Text)
End With
End If
Else
lPos = InStr(1, Me.txtBinary2.Text, strWord, vbTextCompare)
If lPos > 0 Then
With txtBinary2
.SelectionStart = lPos - 1
.SelectionLength = Len(strWord)
.SelectionColor = myColor
.SelectionStart = Len(Me.txtBinary2.Text)
End With
End If
End If
End sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Worked like a champ..tyvm
I think I couldnt get it to work because I was not incrementing the counter. So it just started over again.
I do have a hiccup though, the first time i run the code it works and all 1s are red as desired, if I make a change on my form which triggers the code again, the entire line is red again.
I'm sure I'll figure it out.
MW
I think I couldnt get it to work because I was not incrementing the counter. So it just started over again.
I do have a hiccup though, the first time i run the code it works and all 1s are red as desired, if I make a change on my form which triggers the code again, the entire line is red again.
I'm sure I'll figure it out.
MW
Open in new window