Brokenhope
New member
- Joined
- Sep 24, 2005
- Messages
- 3
- Programming Experience
- 3-5
I have a richtextbox in my api, and I need to do some syntax Highlighting.
I need some SERIOUS help with this, it is extreamly important to my program, Ive spent 2 days researching (found almost nothing), and 2 days writing code. What I came up with works to a point, but theres a lot of flaws, first I will say how my idea works:
-Gets position of first character of current line (to use as a base in Select())
-Selects the whole current line, and changes its font color to black
-Splits current line by space, tab, {, }, (, ), and ;
-Loops through each value in the newly split array, tries to match it to a value in the highlight array, if it matches selects word, and chages color to blue
-Deselects everything, sets pointer back where it started
Heres the problems
-You can see the text flicker in the richtext box (by flicker I mean you can see each word selected for a split second)
-The loops counter gets off with space and tab, because they are an extra character that needs to be added to the current position to select correctly, if either a space or tab is used the whole line fails in highlighting, and highlights the wrong stuff
Heres my questions
-Is there a better way to do this? I heard Edit Pad Pro uses regular expressions, I have no clue how to do that though?
-If my first question cant be answered, then is it possible to figure out what position the current word in the array is in the string ex:
Array { "Bob", "Billy" }
Hey Im Bob
Bob would be 8-11 in the string, but my script thinks 6-9.
If you want to see my full code here: (its quite big and quite messy)
If you need a screenshot of what its doing I can give it to you.
Its weird though ill give you a small little tidbit, with the code above highlighting echo's.
echo echo
It gets more and more off with more spaces :/
I need some SERIOUS help with this, it is extreamly important to my program, Ive spent 2 days researching (found almost nothing), and 2 days writing code. What I came up with works to a point, but theres a lot of flaws, first I will say how my idea works:
-Gets position of first character of current line (to use as a base in Select())
-Selects the whole current line, and changes its font color to black
-Splits current line by space, tab, {, }, (, ), and ;
-Loops through each value in the newly split array, tries to match it to a value in the highlight array, if it matches selects word, and chages color to blue
-Deselects everything, sets pointer back where it started
Heres the problems
-You can see the text flicker in the richtext box (by flicker I mean you can see each word selected for a split second)
-The loops counter gets off with space and tab, because they are an extra character that needs to be added to the current position to select correctly, if either a space or tab is used the whole line fails in highlighting, and highlights the wrong stuff
Heres my questions
-Is there a better way to do this? I heard Edit Pad Pro uses regular expressions, I have no clue how to do that though?
-If my first question cant be answered, then is it possible to figure out what position the current word in the array is in the string ex:
Array { "Bob", "Billy" }
Hey Im Bob
Bob would be 8-11 in the string, but my script thinks 6-9.
If you want to see my full code here: (its quite big and quite messy)
VB.NET:
Public Function rtbTextBoxSyntaxUpdate()
Dim rexRegEx As New Regex("([ \\t{}();])")
Dim i As Integer = 1
Dim ii As Integer
Dim bMatch As Boolean = False
Dim iPosition As Integer
Dim iSelectStart As Integer = 0
Dim iSelectedPositionStart As Integer
'Dim iSelectEnd As Integer
Dim iCurrentLine As Integer
Dim strLine() As String
'Define Highlight array
Dim strHighlight() As String = {"echo", "print", "include", "fopen", "fclose", "function", "array", "true", "false", "if", "else", "elseif"}
'Define numbers
iCurrentLine = SendMessage(rtbTextBox.Handle.ToInt32(), EM_LINEFROMCHAR, -1, 0) + 1
iSelectedPositionStart = rtbTextBox.SelectionStart
'Find position of first character on current line
While i < iCurrentLine
iSelectStart = iSelectStart + rtbTextBox.Lines(i - 1).Length + 1
i = i + 1
End While
'Change line color to black
On Error Resume Next
rtbTextBox.Select(iSelectStart, rtbTextBox.Lines(iCurrentLine - 1).Length)
rtbTextBox.SelectionColor = System.Drawing.Color.Black
i = 0
strLine = rexRegEx.Split(rtbTextBox.Lines(iCurrentLine - 1))
For i = 0 To UBound(strLine)
For ii = 0 To UBound(strHighlight)
If strHighlight(ii) = strLine(i) Then
bMatch = True
End If
Next
If bMatch = True Then
rtbTextBox.Select(iSelectStart + iPosition, strLine(i).Length)
rtbTextBox.SelectionColor = System.Drawing.Color.Blue
End If
rtbTextBox.Select(iSelectedPositionStart, 0)
iPosition = iPosition + strLine(i).Length + 1
bMatch = False
Next
rtbTextBox.Select(iSelectedPositionStart, 0)
rtbTextBox.SelectionColor = System.Drawing.Color.Black
End Function
If you need a screenshot of what its doing I can give it to you.
Its weird though ill give you a small little tidbit, with the code above highlighting echo's.
echo echo
It gets more and more off with more spaces :/