Results 1 to 1 of 1

Thread: rtf Search Backwards

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378

    rtf Search Backwards

    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
    Last edited by GARY MICHAEL; Sep 29th, 2005 at 06:54 AM.
    Thanks,
    GARY

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