VB.NET Syntax Highlighting in a Rich Text Box

I'm coding a simple code editor for a very simple scripting language we use at work. My syntax highlighting code works fine if I do it on the entire RichTextBox (rtbMain) but when I try to get it to work on just that line, so I can run the function with rtbMain changes, it gets weird. I can't seem to figure out why. Am I even going about this the right way?

rtbMain is the main text box. frmColors.lbRegExps is a listbox of words to highlight (later it will have slightly more powerful regular expressions.) frmColor.lbHexColors is another listbox with the corresponding hex colors for the words.

Private Sub HighLight(ByVal All As Boolean)
    Dim RegExp As System.Text.RegularExpressions.MatchCollection
    Dim RegExpMatch As System.Text.RegularExpressions.Match
    Dim FirstCharIndex As Integer = rtbMain.GetFirstCharIndexOfCurrentLine
    Dim CurrentLine As Integer = rtbMain.GetLineFromCharIndex(FirstCharIndex)
    Dim CurrentLineText As String = rtbMain.Lines(CurrentLine)
    Dim CharsToCurrentLine As Integer = rtbMain.SelectionStart
    Dim PassNumber As Integer = 0

    LockWindowUpdate(Me.Handle.ToInt32) 'Let's lock the window so it doesn't scroll all crazy.
    If All = True Then 'Highlight everything.
        For Each pass In frmColors.lbRegExps.Items
            RegExp = System.Text.RegularExpressions.Regex.Matches(LCase(rtbMain.Text), LCase(pass))
            For Each RegExpMatch In RegExp
                rtbMain.Select(RegExpMatch.Index, RegExpMatch.Length)
                rtbMain.SelectionColor = ColorTranslator.FromHtml(frmColors.lbHexColors.Items(PassNumber))
            Next
            PassNumber += 1
        Next
    Else 'Highlight just that row.
        For Each pass In FrmColors.lbRegExps.Items
            RegExp = System.Text.RegularExpressions.Regex.Matches(LCase(CurrentLineText), LCase(pass))
            For Each RegExpMatch In RegExp
                rtbMain.Select(RegExpMatch.Index + (CharsToCurrentLine - RegExpMatch.Length), RegExpMatch.Length)
                rtbMain.SelectionColor = Color.Blue
            Next
        Next
    End If

    rtbMain.Select(CharsToCurrentLine, 0) 'Reset colors and positon and then unlock drawing.
    rtbMain.SelectionColor = Color.Black
    LockWindowUpdate(0)
End Sub

Answers


Ok I figured it out. I was calling the even on rtbMain.TextChange thinking that this would only trigger if the text actually changed. Nay Nay, it will also trigger if formatting is changed. so each time it changed something while it was doing its first pass and highlighting everything, it would then trigger to highlight the line. It would do this until there was nothing left to change.

I set a boolean variable for weather or not it was currently highlighting and added an if condition inside the TextChange sub

P.S. I don't have the self-learner badge so any up ratings would be welcome :P


Need Your Help

My Google Marker is not showing up in Google Maps

javascript jquery google-maps google-maps-api-3

Hey I am trying to create a Google Map using Google's API. The marker is not showing up after I've entered the correct code. Can someone please help me to find out why is is not showing up? I have ...

subclass UILabel to have letter spacing doesn't work with accent letter

iphone ios uilabel uifont

i have found this code here on SO that help me to have a letter spacing in a UILabel, this is the code:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.