Results 1 to 11 of 11

Thread: Syntax Highlighting, highlighting text already there

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Syntax Highlighting, highlighting text already there

    hello, i have a function for highlighting keywords in text and it works fine if your typing keywords, but when i load a file it doesnt highlight the keywords i have that are in the file. in fact after loading a file, even if i type a keyword it doesn't highlight... any help?
    heres the code i have to highlight:
    vb Code:
    1. Private Sub HighlightC(Find As String, Color As OLE_COLOR, MainColor As OLE_COLOR)
    2.     Dim i As Long
    3.     Dim txtBox As String
    4.     Dim intTxtLen As Integer
    5.     Dim intLength As Integer
    6.     Dim intStart As Integer
    7.    
    8.     intLength = Len(Find)
    9.     txtBox = txtLoad.Text
    10.     intTxtLen = Len(txtLoad.Text)
    11.    
    12.     With txtLoad
    13.         For i = 1 To intTxtLen
    14.             If Mid$(txtBox, i) = Find Then
    15.                 .SelStart = i - 1
    16.                 .SelLength = intLength
    17.                 .SelColor = Color
    18.                 .SelStart = intTxtLen
    19.                 .SelColor = MainColor
    20.             End If
    21.         Next
    22.        
    23.     End With
    24. End Sub

  2. #2
    Addicted Member ThorSubak's Avatar
    Join Date
    Apr 2008
    Location
    Cebu
    Posts
    153

    Re: Syntax Highlighting, highlighting text already there

    is your textbox ("txtload") has its additional property?
    specifically [.selcolor]..

  3. #3
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Syntax Highlighting, highlighting text already there

    I assume this is a RichTextBox rather than a TextBox.

    Where are you calling the subroutine?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Re: Syntax Highlighting, highlighting text already there

    yes, it is a richtextbox. i only have txt because i decided to add highlighting after the fact i had the program all coded..
    and i use it on txtload_change. i can't seem to see why its not working...

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Re: Syntax Highlighting, highlighting text already there

    ok. i have gotten this code:
    vb Code:
    1. Private Sub HighlightL(Find As String, Color As OLE_COLOR, MainColor As OLE_COLOR)
    2.  
    3.     Dim i As Long
    4.     Dim txtBox As String
    5.     Dim intTxtLen As Integer
    6.     Dim intLength As Integer
    7.     Dim intPos As Integer
    8.     Dim blnFound As Boolean
    9.     Dim iTemp As Long
    10.    
    11.     intPos = txtLoad.SelStart
    12.     intLength = Len(Find)
    13.     txtBox = txtLoad.Text
    14.     On Error Resume Next
    15.     intTxtLen = Len(txtLoad.Text)
    16.    
    17.     With txtLoad
    18.         For i = 0 To intTxtLen
    19.             intPos = .SelStart
    20.             If .Find(Find, i, i + intLength) = Find Then
    21.                 .Find Find, i, i + intLength
    22.                 .SelColor = Color
    23.                 .SelStart = intPos
    24.                 .SelLength = 0
    25.                 .SelColor = MainColor
    26.             End If
    27.         Next
    28.     End With
    29.    
    30. End Sub

    and it works exactly how i want... one problem.. large files = very very slow. any help on speeding this up?

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Syntax Highlighting, highlighting text already there

    Have you tried loading the file and then calling HighlightL ? (and disabling the Change event until it's complete.) That should speed things up. At the moment you're executing the code for every change made to the RTB (ie as a new character is added)

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Syntax Highlighting, highlighting text already there

    How large a file are you dealing with?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Re: Syntax Highlighting, highlighting text already there

    the file can be any size as the user loads it...
    but i have it in the keydown event and it only calls when a user hits space or enter.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Angry Re: Syntax Highlighting, highlighting text already there

    i have gotten code similar to what i have below to work very fast. but it didnt highlight everything so now i tried to edit it and my prog crashed and i lost my code (i learned..). now in this code i have NO idea why but i always equals 0
    which results in an infinate loop... which is not what i want.
    vb Code:
    1. Private Sub HighlightL(Find As String, Color As OLE_COLOR, MainColor As OLE_COLOR)
    2.  
    3.     Dim i As Long
    4.     Dim txtBox As String
    5.     Dim intTxtLen As Integer
    6.     Dim intLength As Integer
    7.     Dim intPos As Integer
    8.     Dim intPosI As Integer
    9.     Dim blnFound As Boolean
    10.     Dim iTemp As Long
    11.    
    12.     intPos = txtLoad.SelStart
    13.     intLength = Len(Find)
    14.     txtBox = txtLoad.Text
    15.     On Error Resume Next
    16.     intTxtLen = Len(txtLoad.Text)
    17.    
    18.     With txtLoad
    19.        
    20.         Do Until i = intTxtLen
    21.             i = .Find(Find, i, i + intLength)
    22.            
    23.             If Mid(txtBox, i, intLength) = Find Then
    24.                 .SelStart = i
    25.                 .SelLength = intLength
    26.                 .SelColor = Color
    27.                 .SelStart = intPos
    28.                 .SelLength = 0
    29.                 .SelColor = MainColor
    30.             End If
    31.            
    32.             i = i + i
    33.             Debug.Print i
    34.            
    35.         Loop
    36.     End With
    37.    
    38. End Sub

  10. #10
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Syntax Highlighting, highlighting text already there

    First I'd remove the "On Error Resume Next" and I think you'll find that the .Find method is returning -1 (the search item was not found). This will cause the Mid statement to fail since -1 as a starting point for a sub-string is invalid. Since your code is ignoring this error, it continues and i is then incremented by one (ie set to 0) and the RTB is searched again starting at position 0. Hence an infinite loop with i always being 0 when you display it.

    I'd suggest you re-structure slightly, something like this perhaps:

    Code:
    Private Sub HighlightL(Find As String, Color As OLE_COLOR, MainColor As OLE_COLOR)
    
        Dim i As Long
        Dim txtBox As String
        Dim intTxtLen As Integer
        Dim intLength As Integer
        Dim lngPos As Long
        Dim blnFound As Boolean
        Dim iTemp As Long
        
        intPos = txtLoad.SelStart
        intLength = Len(Find)
        txtBox = txtLoad.Text
        intTxtLen = Len(txtLoad.Text)
        
        With txtLoad
            
            Do Until i >= intTxtLen
                lngPos= .Find(Find, i, i + intLength)
                '
                ' Was the search sting found ?
                '
                If lngPos <> - 1 Then
                     '
                     ' Yes - Highlight it
                     '
                    .SelStart = i
                    .SelLength = intLength
                    .SelColor = Color
                    .SelStart = lngPos
                    .SelLength = 0
                    .SelColor = MainColor
                    '
                    ' Set the start point for the next search
                    '
                    i = lngPos + intLen
                Else
                    '
                    ' The search string was not found so
                    ' force the loop to exit
                    ' 
                    i = i + intTxtLen
                End If
                Debug.Print i
                
            Loop
        End With
    End Sub
    EDIT: I don't think it has any bearing on what you're doing but I'd change the variable name "Find" to soemthing else as 'Find' is a method of the RTB and using such names for variables can cause unexpected problems.
    Last edited by Doogle; Apr 9th, 2008 at 11:45 PM.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Re: Syntax Highlighting, highlighting text already there

    ok. i noticed you never assign i a value... but i fixed that up but it STILL doesnt stop!
    it keeps finding the first keyword over and over and over. i have no clue why. its like i = i + 1 isn't working or something... or theres something about .find i dont know?

    eidt: i fixed that up but now it only finds the first keyword.... and this is because i cannot set lngposi to 0... -.-

    vb Code:
    1. Private Sub HighlightL(ByVal Find As String, ByVal Color As OLE_COLOR)
    2.  
    3.     Dim txtBox As String
    4.     Dim intTxtLen As Integer
    5.     Dim intLength As Integer
    6.     Dim lngPos As Long
    7.     Dim lngPosI As Integer
    8.    
    9.     lngPosI = 0
    10.     lngPos = txtLoad.SelStart
    11.     intLength = Len(Find)
    12.     txtBox = txtLoad.Text
    13.     On Error Resume Next
    14.     intTxtLen = Len(txtLoad.Text)
    15.    
    16.     With txtLoad
    17.        
    18.         Do Until lngPosI >= intTxtLen
    19.             lngPosI = .Find(Find, lngPosI, lngPosI + intLength)
    20.  
    21.             If lngPosI <> -1 Then
    22.                 .SelStart = lngPosI
    23.                 .SelLength = intLength
    24.                 .SelColor = Color
    25.                 .SelStart = lngPos
    26.                 .SelLength = 0
    27.                 .SelColor = vbBlack
    28.                 lngPosI = lngPosI + 1
    29.             Else
    30.  
    31.                 lngPosI = lngPosI + intTxtLen
    32.             End If
    33.             Debug.Print lngPosI
    34.  
    35.         Loop
    36.        
    37.     lngPosI = 0
    38.    
    39.     End With
    40.    
    41. End Sub

    Ah Ha! I got it!

    lngPosI = .Find(Find, lngPosI, lngPosI + intLength)

    have to remove lngPosI + intLength from that. Thanks doogle
    Last edited by bluehairman; Apr 10th, 2008 at 07:00 PM.

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