Highlighting - Very Important

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)
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 :/
 
Thanks for the reply, but I couldnt find anything useful there. Find and replace would work, but to a point that I use a regular expression list to match certain things, which would be better so it could highlight like:

print "string";

And actually color strings and such like that, but the find and replace wasnt designed like that, and I only want to attempt to highlight whats on the edited line, not the whole file, which is whats causing a lot of problems, the positioning of whats selected and whats highlighting, and the selecting thing is causing the problem of the "flicker".

I also downloaded the RTBEditor.zip, didnt contain anything useful either. My code works, but only to a point, the regex messed everything up, before I just split by spaces and it worked fine, but I need to have more than just that, because that wont highlight function( of if(, so yea...

It would be great if somehow I could use regex in the actual rtf text, not whats displayed, but the actuall tags the box reads, and just replace directly from that. So I could eaisly color strings, numerics, conditionals, etc, instead of trying to select specific words and color them... any ideas?


EDIT


I fixed my code, turned out the +1 was what was causing it to fail, I guess somehow the split characters were racking up without the +1, so now I have it working, but its not the way I want it to be, I want regular expressions, it would help it so much, and make it way better. Heres a screen of what it looks like in its current state (each color for each command is customizable as well)

build000syntaxhighlighting0.jpg


If the image is down, its because my computers wireless kicked me off, which does happen. Its kinda ugly in its current state, and the slight flicker still is there.
 
Last edited:
Hey brokenhope... Was looking thru the post and saw what you are doing.
I dire need something similar... would it be too much to ask the code
the highlighting text in the editor??

Thx..

LoneFerret
 
Back
Top