VB Help on Find Previous

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • denakaraj
    New Member
    • Jul 2007
    • 14

    #1

    VB Help on Find Previous

    Hi Everyone
    I am new to this forum
    I need a help in VB
    i need a code for Find previous option
    I am having one RichTextBox and two Command buttons [Find Previous and Find Next] in the Form
    I have done the code for Find Next but not able to do for Find Previous Option
    Please help me on this issue
    I need to finish this work very soon
    Thanks in Advance
  • denakaraj
    New Member
    • Jul 2007
    • 14

    #2
    Hi Everyone
    help me on this

    Comment

    • hariharanmca
      Top Contributor
      • Dec 2006
      • 1977

      #3
      Originally posted by denakaraj
      Hi Everyone
      help me on this
      Can you post, what you done in your code and explain little bit more (What you find next and previous). So that we can get what you need.

      Comment

      • danp129
        Recognized Expert Contributor
        • Jul 2006
        • 323

        #4
        Similar to InStr exists:
        Function InStrRev(String Check As String, StringMatch As String, [Start As Long = -1], [Compare As VbCompareMethod = vbBinaryCompare]) As Long

        It will search in reverse direction as the name suggests. Set the "Start" parameter to the textbox's current .SelStart.

        Example (untested):

        Code:
        ret=instrrev(text1.text, "mysearchtext", text1.selstart)
        if ret <> 0 then 
             text1.selstart=ret
             text1.sellen=len("mysearchtext")
        else
             msgbox "Not found."
        end if

        Comment

        • denakaraj
          New Member
          • Jul 2007
          • 14

          #5
          Hi All,
          This is my coding for Find Next option

          Find Next:

          Dim goptions, srch, str, result, start As Variant
          goptions = rtfWholeWord + rtfMatchCase
          srch = Trim(RichTextBo x2.Text)
          RichTextBox1.Se lBold = False
          RichTextBox1.Se lColor = vbBlack
          result = RichTextBox1.Fi nd(srch, start, , goptions)
          If result >= 0 Then
          RichTextBox1.Se lBold = True
          RichTextBox1.Se lColor = vbBlue
          Else
          str = MsgBox("Specifi c Region is Searched, Do you want to continue from the top", vbYesNo, "Informatio n'")
          If str = 7 Then
          Exit Sub
          Else
          Call clear_formattin g
          start = 0
          Exit Sub
          End If
          End If
          start = result + Len(srch)


          Is that possible to have similar kind of coding for Find Previous

          Thanks in Advance

          Comment

          • hariharanmca
            Top Contributor
            • Dec 2006
            • 1977

            #6
            Originally posted by denakaraj
            Hi All,
            This is my coding for Find Next option

            Find Next:

            Dim goptions, srch, str, result, start As Variant
            goptions = rtfWholeWord + rtfMatchCase
            srch = Trim(RichTextBo x2.Text)
            RichTextBox1.Se lBold = False
            RichTextBox1.Se lColor = vbBlack
            result = RichTextBox1.Fi nd(srch, start, , goptions)
            If result >= 0 Then
            RichTextBox1.Se lBold = True
            RichTextBox1.Se lColor = vbBlue
            Else
            str = MsgBox("Specifi c Region is Searched, Do you want to continue from the top", vbYesNo, "Informatio n'")
            If str = 7 Then
            Exit Sub
            Else
            Call clear_formattin g
            start = 0
            Exit Sub
            End If
            End If
            start = result + Len(srch)


            Is that possible to have similar kind of coding for Find Previous

            Thanks in Advance

            i think, the code posted by danap129 will help you and its short.

            Comment

            • denakaraj
              New Member
              • Jul 2007
              • 14

              #7
              Hi

              Will it work with RichTextBox also

              Comment

              • hariharanmca
                Top Contributor
                • Dec 2006
                • 1977

                #8
                Originally posted by denakaraj
                Hi

                Will it work with RichTextBox also

                let you to try somthing(Yes, it will).

                Comment

                • denakaraj
                  New Member
                  • Jul 2007
                  • 14

                  #9
                  Dim result, srch As Variant
                  srch = RichTextBox2.Te xt
                  RichTextBox1.Se lBold = False
                  RichTextBox1.Se lColor = vbBlack
                  result = InStrRev(RichTe xtBox1.Text, srch, RichTextBox1.Se lStart)
                  If result <> 0 Then
                  RichTextBox1.Se lStart = result
                  RichTextBox1.Se lBold = True
                  RichTextBox1.Se lColor = vbGreen
                  Else
                  MsgBox "Not found."
                  End If

                  I have used dnap coding but i couldn't get the result itself please let me know if i missed out anything
                  Thanks in Advance

                  Comment

                  • hariharanmca
                    Top Contributor
                    • Dec 2006
                    • 1977

                    #10
                    Originally posted by denakaraj
                    Dim result, srch As Variant
                    srch = RichTextBox2.Te xt
                    RichTextBox1.Se lBold = False
                    RichTextBox1.Se lColor = vbBlack
                    result = InStrRev(RichTe xtBox1.Text, srch, RichTextBox1.Se lStart)
                    If result <> 0 Then
                    RichTextBox1.Se lStart = result
                    RichTextBox1.Se lBold = True
                    RichTextBox1.Se lColor = vbGreen
                    Else
                    MsgBox "Not found."
                    End If

                    I have used dnap coding but i couldn't get the result itself please let me know if i missed out anything
                    Thanks in Advance


                    okay, don't confuse.

                    i think the below method will work

                    Code:
                    Private Sub SearchMe()
                    goptions = rtfWholeWord + rtfMatchCase
                    srch = Trim(RichTextBox2.Text)
                    RichTextBox1.SelBold = False
                    RichTextBox1.SelColor = vbBlack
                    result = RichTextBox1.Find(srch, start - (Len(Trim(RichTextBox2.Text)
                    ) + 1), , goptions) 
                    If result >= 0 Then
                    RichTextBox1.SelBold = True
                    RichTextBox1.SelColor = vbBlue
                    Else
                    str = MsgBox("Specific Region is Searched, Do you want to continue from the top", vbYesNo, "Information'")
                    If str = 7 Then
                    Exit Sub
                    Else
                    'Call clear_formatting
                    start = 0
                    Exit Sub
                    End If
                    End If
                    start = result - Len(srch)
                    
                    End Sub
                    i think this will help you

                    Comment

                    • danp129
                      Recognized Expert Contributor
                      • Jul 2006
                      • 323

                      #11
                      Code:
                      Option Explicit
                      
                      Private Sub Command1_Click()
                          Dim result As Long, srch As Variant
                          Dim lngStart As Long
                          srch = RichTextBox2.Text
                          
                          'RichTextBox1.SelBold = False
                          'RichTextBox1.SelColor = vbBlack
                          If RichTextBox1.SelStart = 0 Then lngStart = 1 Else lngStart = RichTextBox1.SelStart
                          result = InStrRev(RichTextBox1.Text, srch, lngStart, vbTextCompare)
                          
                          If result <> 0 Then
                              RichTextBox1.SelStart = result - 1
                              RichTextBox1.SelLength = Len(srch)
                              'RichTextBox1.SelBold = True
                              'RichTextBox1.SelColor = vbGreen
                          Else
                              MsgBox "Not found."
                          End If
                      
                      End Sub
                      
                      Private Sub Form_Load()
                          RichTextBox1.HideSelection = False
                      End Sub

                      Comment

                      • denakaraj
                        New Member
                        • Jul 2007
                        • 14

                        #12
                        Hi dnap,
                        I have used ur previous coding and it is working really very fine
                        thanks for the help
                        with your previous code is there any way to find whole word because i am able to case-sensitive search but i cant do whole word
                        please help me on this

                        Find Previous Code:

                        [CODE=vb]Dim result1 As Variant
                        RichTextBox1.Se lBold = False
                        RichTextBox1.Se lColor = vbBlack
                        If RichTextBox1.Se lStart <> 0 Then
                        result1 = InStrRev(RichTe xtBox1.Text, RichTextBox2.Te xt, RichTextBox1.Se lStart, vbTextCompare)
                        End If
                        If result1 <> 0 Then
                        result1 = result1 - 1
                        RichTextBox1.Se lStart = result1
                        RichTextBox1.Se lLength = Len(RichTextBox 2.Text)
                        RichTextBox1.Se lBold = True
                        RichTextBox1.Se lColor = vbBlue
                        result1 = result1 + 1
                        Else
                        str = MsgBox("Specifi c Region is Searched, Do you want to continue from the Bottom", vbYesNo, "Informatio n'")
                        If str = 7 Then
                        Exit Sub
                        Else
                        Call clear_formattin g
                        RichTextBox1.Se lStart = Len(RichTextBox 1.Text)
                        result1 = 1
                        Exit Sub
                        End If
                        End If
                        start = result1 // this is for find next option[/CODE]

                        thanks in advance
                        Last edited by Killer42; Jul 19 '07, 05:02 AM. Reason: Added CODE=vb tag

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          Can you describe in detail what you mean by "finding whole word"? There might be different ways of interpreting this. Or to put it another way, can you describe (preferably showing examples) what situations you would consider to be a "whole word" match, and what you would not?

                          Comment

                          • denakaraj
                            New Member
                            • Jul 2007
                            • 14

                            #14
                            Hi
                            For Example i am having text like below shown in my textbox
                            hi
                            hii
                            If I want to find "hi" with wholeword option then
                            only "hi" has to select and not "hii"

                            Comment

                            • hariharanmca
                              Top Contributor
                              • Dec 2006
                              • 1977

                              #15
                              Originally posted by denakaraj
                              Hi
                              For Example i am having text like below shown in my textbox
                              hi
                              hii
                              If I want to find "hi" with wholeword option then
                              only "hi" has to select and not "hii"

                              just add space front and back of the text which you are getting it from the user.

                              Comment

                              Working...