Ive created a function for Syntax highlighting but when it comes to having to check for over 100 of the same words, it gets to the point where it continues to go and check the words selecting them all and its just not efficient.
So, What is the best way to do this..
here is what i have:
So, What is the best way to do this..
here is what i have:
Code:
Public Function SyntaxHighlight(Textbox As RichTextBox)
Dim wordsRED As New List(Of String)
Dim wordsBLUE As New List(Of String)
Dim wordsPURPLE As New List(Of String)
Dim wordsGREEN As New List(Of String)
'Red Word List
wordsRED.Add("<php")
wordsRED.Add("?>")
'Blue Word List
wordsBLUE.Add("enum")
wordsBLUE.Add("#define ")
wordsBLUE.Add("new ")
wordsBLUE.Add("enums")
wordsBLUE.Add("#includes")
wordsBLUE.Add("return 1;")
'Purple Word List
wordsPURPLE.Add("{")
wordsPURPLE.Add("}")
wordsPURPLE.Add("||")
wordsPURPLE.Add("&&")
'Green Word List
wordsGREEN.Add("//")
wordsGREEN.Add("/*")
wordsGREEN.Add("*/")
If Textbox.Text.Length > 0 Then
Dim selectStart As Integer = Textbox.SelectionStart
Textbox.Select(0, Textbox.Text.Length)
Textbox.SelectionColor = Color.Black
RichTextBox1.DeselectAll()
'Red Colored Words
For Each oneWord As String In wordsRED
Dim pos As Integer = 0
Do While Textbox.Text.ToUpper.IndexOf(oneWord.ToUpper, pos) >= 0
pos = Textbox.Text.ToUpper.IndexOf(oneWord.ToUpper, pos)
Textbox.Select(pos, oneWord.Length)
Textbox.SelectionColor = Color.Red
pos += 1
Loop
Next
Textbox.SelectionStart = selectStart
'Blue Colored Words
For Each oneWord As String In wordsBLUE
Dim pos As Integer = 0
Do While Textbox.Text.ToUpper.IndexOf(oneWord.ToUpper, pos) >= 0
pos = Textbox.Text.ToUpper.IndexOf(oneWord.ToUpper, pos)
Textbox.Select(pos, oneWord.Length)
Textbox.SelectionColor = Color.Blue
pos += 1
Loop
Next
Textbox.SelectionStart = selectStart
'Purple Colored Words
For Each oneWord As String In wordsPURPLE
Dim pos As Integer = 0
Do While Textbox.Text.ToUpper.IndexOf(oneWord.ToUpper, pos) >= 0
pos = Textbox.Text.ToUpper.IndexOf(oneWord.ToUpper, pos)
Textbox.Select(pos, oneWord.Length)
Textbox.SelectionColor = Color.Purple
pos += 1
Loop
Next
Textbox.SelectionStart = selectStart
'Green Colored Words
For Each oneWord As String In wordsGREEN
Dim pos As Integer = 0
Do While Textbox.Text.ToUpper.IndexOf(oneWord.ToUpper, pos) >= 0
pos = Textbox.Text.ToUpper.IndexOf(oneWord.ToUpper, pos)
Textbox.Select(pos, oneWord.Length)
Textbox.SelectionColor = Color.Green
pos += 1
Loop
Next
Textbox.SelectionStart = selectStart
End If
Return 1
End Function