So my program reads a text file into a richtextbox. For example, I want all the words "echo" "pause" "setlocal" to be blue
, "%" to be orange
", "rem" "::" to be green
and so on. A lot of different words get different colors. Yes, the idea is so the richtextbox reads .bat files like notepad++ do color-wise.
I've figured out how to set a color to one word
Private Sub PreviewRTB_TextChanged(sender As Object, e As EventArgs) Handles PreviewRTB.TextChanged
PreviewRTB.Multiline = True
PreviewRTB.ScrollBars = ScrollBars.Vertical
'Set Colors
Dim index As Integer = 0
Dim c_blue As String = "Echo"
While index <> -1
index = PreviewRTB.Find(c_blue, index, RichTextBoxFinds.WholeWord)
If index <> -1 Then
PreviewRTB.SelectionStart = index
PreviewRTB.SelectionLength = c_blue.Length
PreviewRTB.SelectionColor = Color.Blue
index += c_blue.Length
End If
End While
But where do I set the rest of the words under dim c_blue? I tried Dim c_blue As String = "Echo" & "setlocal" & "pause"
also tried to use +
between as well, but it doesn't work. And how do I progress to add another color to other words? Thanks!
For simple coloring, provided that the text isn't too long, something like this will do:
The idea here is pretty straight forward:
String.Split
word by word (note that the condition "RTB text is changed" can be changed with anything you want)word.Length
+1
for the whitespaceThe result will be something like this:
Next (2)
Next (3)
Next (4)
Next (5)