I am making a parsing feature. The parsing works great and selects the right text. The problem is changing the color of the selected text in a richtextbox.
I store 2 words in a hashtable (Start word,End Word)
and use an arraylist to hold colors
I can change the text color of the selected words but its always to the first color in the arraylist even though it should also use the other colors and in fact should use the other colors but wont.
here is the code im using
Rich=richtextbox
vb
Sub Highlight(ByVal Input As String)
Rich.Text = Input 'Move the text into the richtextbox
Dim Text_Start As Integer = 0 'The start of the text to find
Dim Text_End As Integer = 0 'The end of the text to find
Dim Text_Length As Integer = 0 'The Length of the word
Dim a As Integer = 0 'for looping though the hash table & colors
'Txt_highlight = hashtable with start word & end word
For a = 0 To Txt_Highlight.Count - 1 'Use for to loop though hash table
Do 'use do to loop infinitly though text
'Fill text_start with the start location of text to select.
Text_Start = Rich.Text.IndexOf(Txt_Highlight.Keys(a).ToString, Text_End)
If Text_Start = -1 Then Exit Do 'if text not found exit do
'Fill text_end with the end location of text to select.
Text_End = Rich.Text.IndexOf(Txt_Highlight.Values(a).ToString, Text_Start) + Txt_Highlight.Values(a).ToString.Length
If Text_End = -1 Then Exit Do 'if text not found exit do
Text_Length = Text_End - Text_Start 'Calculate Length
If Text_Length <= -1 Then 'If the text_length is negitive exit do
Exit Do
Else 'if its not then do below
Rich.Select(Text_Start, Text_Length) 'select the text
Rich.SelectionColor = Txt_Color.Item(a) 'Color the text
Rich.DeselectAll() 'Deselect all for next selection
Rich.SelectionColor = Color.Black 'Reset color
End If
Loop 'loop to the next match further down
Next 'loop to the next words to find
Rich.Update() 'Make sure the richtextbox is updated
End Sub
Can any one help? i have been searching the internet before i went and posted here but none of the things i have tried worked.
-=Solved=-
I finally figured it out. what i forgot to do was to reset text_start & text_end. because of this when it tried adding more colors it was already at the end of the richtextbox and wouldnt add them. all i needed to add was
vb
For a = 0 To Txt_Highlight.Count - 1 'Use for to loop though hash table
Text_Start = 0 'needed this to reset
Text_End = 0 'needed this to reset
I hope my mistake will keep others from repeating it.
This post has been edited by sired22: 9 Aug, 2008 - 02:56 AM