Searching Text --Need Help

PureSc0pe

Well-known member
Joined
Mar 17, 2004
Messages
161
Location
Arizona
I am making a program where you enter the info to search into a textbox, then you press a button and it displays the text into a listbox. The code I have so far searches the file but it makes it to where I have to enter a certain amount of letters to search. Where it says (0, 7) I can only search if I put in seven letters, but how do I make it search without having a text limit? Please Help.

The Coding I have so far:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str, any, str2 As String
Dim sr As System.IO.StreamReader
Dim prog As System.IO.File
any = TextBox1.Text
sr = prog.OpenText("thelist.txt")
While sr.Peek <> -1
str = sr.ReadLine
str2 = str.Substring(0, 4)
If any.ToLower = str2.ToLower Then
ListBox1.Items.Add(str)
End If
End While
End Sub
 
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str, any, str2 As String
Dim sr As System.IO.StreamReader
Dim prog As System.IO.File
any = TextBox1.Text
sr = prog.OpenText("thelist.txt")
While sr.Peek <> -1
str = sr.ReadLine
str2 = str.Substring(0, textbox1.Text.Length) changed
If any.ToLower = str2.ToLower Then
ListBox1.Items.Add(str)
End If
End While
End Sub

Maybe something like that im not sure...
 
Thank you very much, that worked...However, when you run the search it works, but when you run a second one it leaves the old results and puts the new results below it. How do you make it erase the old results and only display the new results? Also, when there are no results I need a message box to pop up saying "No Results Found."

The Code I have:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str, any, str2 As String
Dim sr As System.IO.StreamReader
Dim prog As System.IO.File
any = TextBox1.Text
sr = prog.OpenText("thelist.txt")
While sr.Peek <> -1
str = sr.ReadLine
str2 = str.Substring(0, textbox1.Text.Length)
If any.ToLower = str2.ToLower Then
ListBox1.Items.Add(str)
End If
End While
End Sub

I have the Message Box code I want to use, but how do I put it into the code to make it display the message when there are no results found?

MessageBox.Show("No Results Found", "Search Program", MessageBoxButtons.OK, MessageBoxIcon.Warning)
 
Last edited by a moderator:
You mean earase from the listbox? Call the Clear method of the Items property to do that:
Code:
 YourListBox.Items.Clear()
If you want to know how many items there are in the listbox (one way to check if there was no matches) then use the Count property of the ListBoxs Items property.
Code:
 If YourListBox.Items.Count = 0 Then no matches
 
Thanks a lot for the code, that worked Great! I have just one more question left. In the text file that it is searching, there is a name followed by a : and numbers. The program searches when you put the name in, but does not when you enter the numbers after it. It states the "No Searches Found" Message Box instead. Name:12.34.56.78 is a sample format...

Here is the current Code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
Dim str, any, str2 As String
Dim sr As System.IO.StreamReader
Dim prog As System.IO.File
any = TextBox1.Text
sr = prog.OpenText("thelist.txt")
While sr.Peek <> -1
str = sr.ReadLine
str2 = str.Substring(0, textbox1.Text.Length)
If any.ToLower = str2.ToLower Then
ListBox1.Items.Add(str)
End If
End While
If ListBox1.Items.Count = 0 Then
MessageBox.Show("No Results Found", "Program Name", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Sub


The program searches when you put the name in, but does not when you enter the numbers after it. It states the "No Results Found" Message Box instead. Name:12.34.56.78 is a sample format...

Also, how would I go about making the enter key start the Search?
 
Last edited by a moderator:
How would I make the enter key enable the search to begin. Also, what would I ahve to do to the search code to enable the numbers after the colon to be searchable as well?
 
Im not sure if im understaind your problem the right way. You are searching a file for the name that the user gives you right? But you didnt say what you want to do with that name. Do you want to find it and parse it for the numbers or something? Sorry If I misunderstood this completely :).

To make enter start the search, go to your forms properties and set its accept button property to the button that has the search code.
 
Sorry if I confused you there. In the text file everthing is done like this, its a sample:

Nickname:12.34.56.78

When I run the search using the name it works fine, but when I search by any of the numbers it doesnt return any results. Is it because theyre after the colon? I need to be able to search by the name and the numbers. I hope that clears things up. Again, any help is appreciated. :)

My Code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
Dim str, any, str2 As String
Dim sr As System.IO.StreamReader
Dim prog As System.IO.File
any = TextBox1.Text
sr = prog.OpenText(TextBox2.Text)
While sr.Peek <> -1
str = sr.ReadLine
str2 = str.Substring(0, textbox1.Text.Length)
If any.ToLower = str2.ToLower Then
ListBox1.Items.Add(str)
End If
End While
If ListBox1.Items.Count = 0 Then
MessageBox.Show("No Results Found", "Program Name", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Sub
 
It doesnt work when I try surching with the numbers, any suggestions?
 
Last edited by a moderator:
Im not sure why you are using substring, but with Substring(0, #), you are looking at the beginning of the string, no matter what you put into the # as the second parameter.
Instead you can use IndexOf to see if the number is in the string. If it is, the IndexOf will return a number that is 0, 1, 2, (non-negative integer).
If not, then IndexOf returns -1. :)
 
Iceplug said:
Im not sure why you are using substring, but with Substring(0, #), you are looking at the beginning of the string, no matter what you put into the # as the second parameter.
Instead you can use IndexOf to see if the number is in the string. If it is, the IndexOf will return a number that is 0, 1, 2, (non-negative integer).
If not, then IndexOf returns -1. :)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
Dim str, any, str2 As String
Dim sr As System.IO.StreamReader
Dim prog As System.IO.File
any = TextBox1.Text
sr = prog.OpenText(TextBox2.Text)
While sr.Peek <> -1
str = sr.ReadLine
str2 = str.indexof(0, 25) Changed
If any.ToLower = str2.ToLower Then
ListBox1.Items.Add(str)
End If
End While
If ListBox1.Items.Count = 0 Then
MessageBox.Show("No Results Found", "Program Name", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Sub

That is the Code I enter based on what I enterpreted from your post. Is that what you meant, because when you set two numbers in the quotations, it requires you to search by that amount of letters. Therefore, that didnt work.
 
Back
Top