|
-
Jun 16th, 2010, 03:09 PM
#1
Thread Starter
New Member
[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.
-
Jun 16th, 2010, 07:14 PM
#2
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:
Public Class Form1
Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
Dim myIndexOf As Integer
myIndexOf = RichTextBox1.Find(txtSearchText.Text, RichTextBoxFinds.MatchCase)
Do Until myIndexOf < 0
'highlight current match
RichTextBox1.SelectionBackColor = Color.Aquamarine
RichTextBox1.SelectionColor = Color.Red
'try to find next occurence
myIndexOf = RichTextBox1.Find(txtSearchText.Text, myIndexOf + txtSearchText.Text.Length, RichTextBoxFinds.MatchCase)
Loop
End Sub
End Class
-
Jun 16th, 2010, 07:52 PM
#3
Thread Starter
New Member
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.
-
Jun 17th, 2010, 06:02 AM
#4
Re: Search rich text box with results
Thread moved from 'VB6 and Earlier' forum to 'VB.Net' (VB2002 and later) forum
-
Jun 17th, 2010, 06:37 AM
#5
Frenzied Member
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
-
Jun 17th, 2010, 02:17 PM
#6
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|