PDA

Click to See Complete Forum and Search --> : rtf Search Backwards


GARY MICHAEL
Jan 9th, 2003, 02:28 PM
Hello all,

I found this code on this site. It works well in searching backwards, but how can I get it to start the search from the cursor position and search backwards? It always starts from the end of the document.


You can use the Start and End parameters of the Find method to write a routine to reverse the search for you, i.e.

visual basic code:

Option Explicit

Private Sub Form_Load()
RichTextBox1.LoadFile "C:\SomeTextFile.txt", rtfText
End Sub

Private Sub Command1_Click()
Static lLastFind As Long
lLastFind = FindRev("the", rtfWholeWord, lLastFind - 1)
Debug.Print lLastFind
End Sub

' This Function performs a Reverse Find on the contents of a RichTextBox
Private Function FindRev(ByVal sFind As String, Optional ByVal enmOptions As FindConstants, Optional ByVal lStart As Long = -1) As Long
Dim lCharIndex As Long
Dim lFound As Long

With RichTextBox1
' If nothing was passed for the "lStart" parameter,
' Default to starting from the End of the Text.
If lStart = -1 Then lStart = Len(.Text)
' Work our way back character by character from the
' Designated starting position to the beginning of the text
For lCharIndex = lStart To 1 Step -1
' Perform a standard search, only starting from
' current Char position, to the starting point
lFound = .Find("the", lCharIndex, lStart, enmOptions)
' If a match was found, exit the loop
If lFound <> -1 Then Exit For
Next
End With
' Return the result
FindRev = lFound
End Function