Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

It's not impossible, it's Inevitable.
Posted

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 i'm not sure...

Posted (edited)

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)

Edited by PureSc0pe
It's not impossible, it's Inevitable.
  • *Experts*
Posted

You mean earase from the listbox? Call the Clear method of the Items property to do that:

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 ListBox's Items property.

If YourListBox.Items.Count = 0 Then 'no matches

Posted (edited)

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?

Edited by PureSc0pe
It's not impossible, it's Inevitable.
Posted
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?
It's not impossible, it's Inevitable.
  • *Experts*
Posted

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 didn't 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 form's properties and set its accept button property to the button that has the search code.

Posted

Sorry if I confused you there. In the text file everthing is done like this, it's 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 doesn't return any results. Is it because they're 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's not impossible, it's Inevitable.
  • Leaders
Posted

I'm 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, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
I'm 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 didn't work.

It's not impossible, it's Inevitable.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...