Results 1 to 6 of 6

Thread: [Resolved] Search rich text box with results

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    9

    [Resolved] Search rich text box with results

    Hi, I'm sort of new to visual basic and I just cant seem to figure out how to do this.

    So I have a search box, a box with a bunch of data, and a results box. I'm trying to figure out how to get it so you can search for something and the results are displayed in the results box (look at image below for an easier explanation)



    all I've been able to do so far is get it to highlight the first line with a result on it. I've tried doing this for a few hours, googled it hundreds of times and I couldn't find anything
    Last edited by Billybombill; Jun 17th, 2010 at 02:56 PM.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Search rich text box with results

    Based on screenshot you are using VB.Net (aka VB 2005/2008/2010) so you need to post this in VB.Net forum instead.

    Anyway, if you are asking how to search and highlight matching words in RTB then try this quick (and I mean very quick) snippet:
    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
    4.         Dim myIndexOf As Integer
    5.  
    6.         myIndexOf = RichTextBox1.Find(txtSearchText.Text, RichTextBoxFinds.MatchCase)
    7.  
    8.         Do Until myIndexOf < 0
    9.             'highlight current match
    10.             RichTextBox1.SelectionBackColor = Color.Aquamarine
    11.             RichTextBox1.SelectionColor = Color.Red
    12.             'try to find next occurence
    13.             myIndexOf = RichTextBox1.Find(txtSearchText.Text, myIndexOf + txtSearchText.Text.Length, RichTextBoxFinds.MatchCase)
    14.         Loop
    15.  
    16.     End Sub
    17. End Class

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    9

    Re: Search rich text box with results

    Thank you using your script and my little knowledge of VB I got it to display the way I wanted it to. Thanks again for the help (and sorry about the wrong section)



    Edit: Small problem. It lists the results right, but lets say theres 5 different lines with the word "test" in them. If you search for "test" it will show the first result 5 times, one for every result. I can't get it to list each result individually. Any ideas?

    My current code

    Code:
        Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim index As Integer = Me.RichTextBox1.Find(searchtext.Text)
            Dim lineindex As Integer = Me.RichTextBox1.GetLineFromCharIndex(index)
            Dim first As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex)
            Dim last As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex + 1)
            Dim myIndexOf As Integer
    
    
            myIndexOf = RichTextBox1.Find(searchtext.Text, RichTextBoxFinds.NoHighlight)
    
    
            Do Until myIndexOf < 0
                'highlight current match
    
                Me.RichTextBox1.Select(first, last - first)
                results.Text = RichTextBox1.SelectedText + results.Text
    
    
    
                myIndexOf = RichTextBox1.Find(searchtext.Text, myIndexOf + searchtext.Text.Length, RichTextBoxFinds.NoHighlight)
            Loop
        End Sub
    Last edited by Billybombill; Jun 16th, 2010 at 08:15 PM.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Search rich text box with results

    Thread moved from 'VB6 and Earlier' forum to 'VB.Net' (VB2002 and later) forum

  5. #5
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: Search rich text box with results

    you can use something like this

    Code:
    For Each s As String In Me.RichTextBox1.Lines
                If s.Contains(TextBox1.Text) Then Me.ListBox1.Items.Add(s)
            Next

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    9

    Re: Search rich text box with results

    YES thank you, 3 lines of code and it works perfectly

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