View Single Post
Old Sep 24th, 2005, 10:53 AM   #3
brokenhope
Hobbyist Programmer
 
Join Date: Apr 2005
Posts: 126
Rep Power: 4 brokenhope is on a distinguished road
I have it working

        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"}

        '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
        rtbTextBox.Select(iSelectStart, rtbTextBox.Lines(iCurrentLine - 1).Length)
        rtbTextBox.SelectionColor = System.Drawing.Color.Black

        i = 0

        strLine = Split(rtbTextBox.Lines(iCurrentLine - 1), " ", -1, CompareMethod.Text)
        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)
            rtbTextBox.SelectionColor = System.Drawing.Color.Black
            iPosition = iPosition + strLine(i).Length + 1
            bMatch = False
        Next

    End Function

Its a piece of junk, i can hardly even understand what I was doing in some places because its so disorganized, but it works. It updates the current line every time a character has been changed with new systax highlighting,

1. Changes whole line black (default typing color) by selecting it and changing select color to black.
2. Searches each individual word (the split will soon be a regex to highlight functions and strings and integers), if it finds a match gets its position, selects the word, changes select word to blue (soon to be customizable colors).
3. Its done

The problem is the line flickers every time you type a letter, by that I mean you can see the whole line selected and each matching word selected for a split second... other syntax highlighters dont do that. Any way I can fix this?
brokenhope is offline   Reply With Quote