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:
Private Sub HighlightC(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 intStart As Integer
intLength = Len(Find)
txtBox = txtLoad.Text
intTxtLen = Len(txtLoad.Text)
With txtLoad
For i = 1 To intTxtLen
If Mid$(txtBox, i) = Find Then
.SelStart = i - 1
.SelLength = intLength
.SelColor = Color
.SelStart = intTxtLen
.SelColor = MainColor
End If
Next
End With
End Sub
Re: Syntax Highlighting, highlighting text already there
is your textbox ("txtload") has its additional property?
specifically [.selcolor]..
Re: Syntax Highlighting, highlighting text already there
I assume this is a RichTextBox rather than a TextBox.
Where are you calling the subroutine?
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...
Re: Syntax Highlighting, highlighting text already there
ok. i have gotten this code:
vb 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 intPos As Integer
Dim blnFound As Boolean
Dim iTemp As Long
intPos = txtLoad.SelStart
intLength = Len(Find)
txtBox = txtLoad.Text
On Error Resume Next
intTxtLen = Len(txtLoad.Text)
With txtLoad
For i = 0 To intTxtLen
intPos = .SelStart
If .Find(Find, i, i + intLength) = Find Then
.Find Find, i, i + intLength
.SelColor = Color
.SelStart = intPos
.SelLength = 0
.SelColor = MainColor
End If
Next
End With
End Sub
and it works exactly how i want... one problem.. large files = very very slow. any help on speeding this up?
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)
Re: Syntax Highlighting, highlighting text already there
How large a file are you dealing with?
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.
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:
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 intPos As Integer
Dim intPosI As Integer
Dim blnFound As Boolean
Dim iTemp As Long
intPos = txtLoad.SelStart
intLength = Len(Find)
txtBox = txtLoad.Text
On Error Resume Next
intTxtLen = Len(txtLoad.Text)
With txtLoad
Do Until i = intTxtLen
i = .Find(Find, i, i + intLength)
If Mid(txtBox, i, intLength) = Find Then
.SelStart = i
.SelLength = intLength
.SelColor = Color
.SelStart = intPos
.SelLength = 0
.SelColor = MainColor
End If
i = i + i
Debug.Print i
Loop
End With
End Sub
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.
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:
Private Sub HighlightL(ByVal Find As String, ByVal Color As OLE_COLOR)
Dim txtBox As String
Dim intTxtLen As Integer
Dim intLength As Integer
Dim lngPos As Long
Dim lngPosI As Integer
lngPosI = 0
lngPos = txtLoad.SelStart
intLength = Len(Find)
txtBox = txtLoad.Text
On Error Resume Next
intTxtLen = Len(txtLoad.Text)
With txtLoad
Do Until lngPosI >= intTxtLen
lngPosI = .Find(Find, lngPosI, lngPosI + intLength)
If lngPosI <> -1 Then
.SelStart = lngPosI
.SelLength = intLength
.SelColor = Color
.SelStart = lngPos
.SelLength = 0
.SelColor = vbBlack
lngPosI = lngPosI + 1
Else
lngPosI = lngPosI + intTxtLen
End If
Debug.Print lngPosI
Loop
lngPosI = 0
End With
End Sub
Ah Ha! I got it!
lngPosI = .Find(Find, lngPosI, lngPosI + intLength)
have to remove lngPosI + intLength from that. Thanks doogle :)