Question : vb.net RichTextBox, chaning text color?

Hi

I want to change some words in coloured words in a Richtextbox. ( like a code editor )

I want to change the important words, like functions such as "WHERE", "FROM", "ORDER BY"

Im using this code now
-------------------------
        On Error Resume Next
        rt.SelectionStart = rt.Find("WHERE")
        rt.SelectionColor = Color.Blue

        rt.SelectionStart = rt.Find("FROM")
        rt.SelectionColor = Color.Blue

        rt.SelectionStart = rt.Find("ORDER BY")
        rt.SelectionColor = Color.Blue
-------------------------
rt is the RichTextBox.
But my problem is, it is only changing the words one time, you see, I want
to have all words coloured, not just once.

Any clue?

Thanks in Advance

Answer : vb.net RichTextBox, chaning text color?

It is because iot only finds first occurence of the word. You can use regular expression to select all occurances.

Below is one such example.

It is copied from another website

Credit goes to original poster.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
Dim myMatches As MatchCollection
 
Dim myPatterns As String() = {"(Purchase Cost:)", "(Sales Cost)"}
 
Dim myColors As Color() = {Color.Blue, Color.Red}
 
RichTextBox1.SelectAll()
 
RichTextBox1.SelectionColor = Color.Black
 
 
For i As Integer = 0 To myPatterns.Length - 1 Step 1
 
myMatches = New Regex(myPatterns(i), RegexOptions.Singleline).Matches(RichTextBox1.Text)
 
For j As Integer = 0 To myMatches.Count - 1 Step 1
 
RichTextBox1.Select(myMatches(j).Groups(1).Index, myMatches(j).Groups(1).Length)
 
RichTextBox1.SelectionColor = myColors(i)
 
Next
 
Next
Open in New Window Select All
Random Solutions  
 
programming4us programming4us