Search in a rich textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BarryM
    New Member
    • Mar 2008
    • 32

    #1

    Search in a rich textbox

    could somebody please tell me how to search a rich textbox for a particular word and then highlight that word in the rich textbox
    ;
    ;
    on the form theres are textbox called txtsearch, a button called btnsearch and a rich textbox called rtbtext. The person enters a word into the textbox, presses the button and then it highligts that word in the richtextbox like the crtl F function in word and notebook etc.
  • gobblegob
    New Member
    • Dec 2007
    • 133

    #2
    Originally posted by BarryM
    could somebody please tell me how to search a rich textbox for a particular word and then highlight that word in the rich textbox
    ;
    ;
    on the form theres are textbox called txtsearch, a button called btnsearch and a rich textbox called rtbtext. The person enters a word into the textbox, presses the button and then it highligts that word in the richtextbox like the crtl F function in word and notebook etc.

    Hi Barry M,
    i dont know what version VB you are using but this is for
    VB 6

    Code: ( vb 6 )
    Code:
    Private Sub Command1_Click()
        Dim Search, Where
        ' Get search string from user.
            Search = Text1.Text 'this is the text to search
        ' Find string in text.
        Where = InStr(RichTextBox1.Text, Search)
        If Where Then
            RichTextBox1.SetFocus
            RichTextBox1.SelStart = Where - 1
            RichTextBox1.SelLength = Len(Search)
        Else
            MsgBox "String not found."
        End If
    End Sub
    GobbleGob.

    Comment

    • BarryM
      New Member
      • Mar 2008
      • 32

      #3
      anybody got the coding for vb 2005

      Comment

      • gobblegob
        New Member
        • Dec 2007
        • 133

        #4
        Originally posted by BarryM
        anybody got the coding for vb 2005
        this works in vb2008

        Code:
            'Make sure you declare this
            Private intPosition As Integer
        
        
            Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                Static intStart As Integer
        
                'used to select compare method
                Dim objType As Object
                objType = CompareMethod.Text
        
                'set starting position to 1
                intPosition = 1
                'use the InStr function to look up a staring position of a search string in a given text box using objType (case-insensitive or case-sensitive)
                intStart = InStr(intPosition, Me.txtText.Text, Me.txtSearch.Text, objType)
        
                If intStart > 0 Then
                    'set starting select position on a textbox and select the search string
                    Me.txtText.SelectionStart = intStart - 1
                    Me.txtText.SelectionLength = Me.txtSearch.Text.Length 'highlights the searched word
                    Me.txtText.Select()
                End If
            End Sub
        GobbleGob.

        Comment

        • BarryM
          New Member
          • Mar 2008
          • 32

          #5
          thanx that works but do you know how to make if it that the person clicks it again it goes to the next matching word in the textbox

          Comment

          • QVeen72
            Recognized Expert Top Contributor
            • Oct 2006
            • 1445

            #6
            Hi,

            for that, You will need to Check if any Word is already selected in the RichTextbox .. (SelLength Property), If already selected, then start Search from (SelStart+SelLe ngth) position instead of First position...

            Regards
            Veena

            Comment

            Working...