Results 1 to 6 of 6

Thread: [RESOLVED] Problem with Syntax Highlighting?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    8

    Resolved [RESOLVED] Problem with Syntax Highlighting?

    Hey guys! This is my first post on the forum so please excuse me if I do something wrong. I've posted this on many forums, however, no one has seemed to give me an answer. So hopefully you guys can help.
    Here is my existing Syntax Highlighting code :
    Code:
        Dim listbox1 As New ListBox
        listbox1.Items.Add("<br/>")
        Dim int As Integer = 0
        Dim line As String = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
        For Each item In listbox1.Items
          If RichTextBox1.Lines(line).Contains(item) Then
            Dim instance As Integer = RichTextBox1.Find(item, RichTextBox1.SelectionStart - CInt(item.ToString.Length), RichTextBox1.SelectionStart)
            If instance > 0 Then
              RichTextBox1.SelectionStart = RichTextBox1.SelectionStart - CInt(item.ToString.Length)
              RichTextBox1.SelectionLength = item.ToString.Length
              RichTextBox1.SelectionColor = Color.Blue
            End If
          End If
    
        Next
        RichTextBox1.SelectionStart = ss
        RichTextBox1.SelectionLength = sl
        RichTextBox1.SelectionColor = Color.Black
    The problem I'm having, is this code also colors around 4 characters after the instance. This confuses me. Any help would be appreciated!

  2. #2
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Problem with Syntax Highlighting?

    Hi, I have just tested your code, and i'm not sure what you want it to do.

    At the moment it just changes the colour of the 4 characters directly before the caret position in your RichTextBox.

    I'm guessing you are trying to highlight the next occurence of the <br/> tag by searching from the caret position onwards?

    Or am I totally off base here?

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    8

    Re: Problem with Syntax Highlighting?

    You're correct, I want it to search for the <br/> command as the user types, so everything from the caret position onward.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    8

    Re: Problem with Syntax Highlighting?

    I know it's bad to bump, but I really need this problem fixed.

  5. #5
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Problem with Syntax Highlighting?

    Hi, Sorry to leave you hanging, I lost track of my posts.. anyhoo...

    Going off what you have said - this would assume that the user is typing into the richtextbox, but not at the end of the text. So if the textbox contained the following text already:

    "This is a text string ending with </br>."

    And the user started typing at the beggining (before the word "This") then you would have to search forwards in the string in order to highlight <br/>. If this is correct and what you are trying to do then you could do this:
    VB Code:
    1. Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
    2.         Dim searchString As String = "<br/>" 'Set the searchString
    3.         Dim cursorPos As Integer = RichTextBox1.SelectionStart 'Get the position of the Textbox Caret
    4.         Dim TextToSearch As String = RichTextBox1.Text.Substring(cursorPos) 'Get a substring (Caret position onwards) to search
    5.  
    6.         If TextToSearch < searchString Then Exit Sub 'The string cannot possibly contain <br/> as it is too short, so we can exit the routine
    7.         For i As Integer = 0 To TextToSearch.Length - searchString.Length
    8.             'See if the TextToSearech contains the first letter of the searchString e.g. "<"
    9.             If TextToSearch.Chars(i) = searchString.Chars(0) Then
    10.                 'If it does then we can try and match the rest of the characters in the searchstring
    11.                 Dim iX As Integer = i
    12.                 Dim isMatch As Boolean = True
    13.                 For Each c As Char In searchString
    14.                     If TextToSearch.Chars(iX) <> c Then
    15.                         'If any of the other letters do not match, then we can drop into the outer For-Loop to look for the next "<"
    16.                         isMatch = False
    17.                         Exit For
    18.                     Else : iX += 1
    19.                     End If
    20.                 Next
    21.                 'Otherwise if all the letters match, we will end up here
    22.                 If isMatch = True Then
    23.                     'So now you can select the text using the index of "<" which is (i) and the cursorpos (because we grabbed a searchstring to do the search)
    24.                     RichTextBox1.SelectionStart = i + cursorPos
    25.                     RichTextBox1.SelectionLength = Len(searchString)
    26.                     RichTextBox1.SelectionColor = Color.Blue
    27.                     Exit For
    28.                 End If
    29.             End If
    30.         Next
    31.     End Sub

    However if you are actually trying to Highlight the word <br/> as the user types it in, then you would need to look backwards in the string like this:
    VB Code:
    1. Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
    2.         Dim searchString As String = "<br/>" 'Set the searchString
    3.         Dim cursorPos As Integer = RichTextBox1.SelectionStart 'Get the position of the Textbox Caret
    4.         If cursorPos < searchString.Length Then Exit Sub 'If the text before the searchString is not long enough to contain it then skip the routine
    5.         Dim TextToSearch As String = RichTextBox1.Text.Substring(cursorPos - Len(searchString), Len(searchString)) 'Get the 5 letters before the Caret
    6.  
    7.         'If TextToSearch matches the searchstring then we know the user has just finished typing it
    8.         If TextToSearch = searchString Then
    9.             'Do your highlighting
    10.             RichTextBox1.SelectionStart = cursorPos - Len(searchString)
    11.             RichTextBox1.SelectionLength = Len(searchString)
    12.             RichTextBox1.SelectionColor = Color.Blue
    13.         End If
    14.     End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    8

    Re: Problem with Syntax Highlighting?

    Ah okay! Thanks alot for posting the 2 code snippets! Both of them will help me tremendously.
    I figured out what I was doing wrong in my first code. So I have replaced that specific code with the second one you posted, except I modified it to support multiple words at once. Seen here :
    Code:
    Dim listbox As New ListBox
            listbox.Items.Add("<br/>")
            For Each item In listbox.Items
                Dim searchString As String = item 'Set the searchString
                Dim cursorPos As Integer = RichTextBox1.SelectionStart 'Get the position of the Textbox Caret
                If cursorPos < searchString.Length Then Exit Sub 'If the text before the searchString is not long enough to contain it then skip the routine
                Dim TextToSearch As String = RichTextBox1.Text.Substring(cursorPos - Len(searchString), Len(searchString)) 'Get the 5 letters before the Caret
                'If TextToSearch matches the searchstring then we know the user has just finished typing it
                If TextToSearch = searchString Then
                    'Do your highlighting
                    RichTextBox1.SelectionStart = cursorPos - Len(searchString)
                    RichTextBox1.SelectionLength = Len(searchString)
                    RichTextBox1.SelectionColor = Color.Blue
                End If
                RichTextBox1.SelectionStart = ss
                RichTextBox1.SelectionLength = sl
                RichTextBox1.SelectionColor = Color.Black
            Next
    But thanks for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width