Searching a text file for a string
Searching a text file for a string
Searching a text file for a string
Searching a text file for a string
Searching a text file for a string
Searching a text file for a string Searching a text file for a string Searching a text file for a string Searching a text file for a string Searching a text file for a string Searching a text file for a string Searching a text file for a string Searching a text file for a string
Searching a text file for a string Searching a text file for a string
Searching a text file for a string
Go Back  Xtreme Visual Basic Talk > > > Searching a text file for a string


Reply
 
Thread Tools Display Modes
  #1  
Old 10-24-2011, 07:49 PM
SuperDav21 SuperDav21 is offline
Newcomer
 
Join Date: Oct 2011
Posts: 1
Default Searching a text file for a string


Hi everyone!

I'm very new to VB and I'm in need of some guidance. I've created a form that gets a user's input (text boxes) and stores it in a structure. That structure is then written to a text file. I've got that all down, but I'm having trouble with a search feature...

I would like to place a button that, when pressed, takes either the input from textbox1 or textbox2, searches the text file for the same string match, and then places the search findings into either a list box, message box, etc.

Basically...the form stores a customer's information to a text file, and I would like the search feature to search within the text file for the last name, or customer number, that is input in the name or customer number text box field, and then post the correct results of the customer to a list box.

I hope that makes some sense to someone out there lol! Any help would be greatly appreciated!


For reference, here is the code I have so far:

Code:
 Dim reader As StreamReader
        Dim strNumber As String = txtNumber.Text
        Dim strLast As String = txtLast.Text
        Dim strCustomer As String = IO.File.ReadAllText("Customer Accounts.txt")

        Try

            reader = File.OpenText("Customer Accounts.txt")


            If strCustomer.IndexOf(strLast) <> -1 Then
                MessageBox.Show("Customer " & txtLast.Text & " was found within the file")


                reader.ReadLine()
                strLast = reader.ReadLine()
                strNumber = reader.ReadLine()

                lstCustomers.Items.Add("Customer's Last Name: " & strLast)
                lstCustomers.Items.Add("Customer's number: " & strNumber)

            ElseIf strCustomer.IndexOf(strNumber) <> -1 Then
                MessageBox.Show("Customer " & txtNumber.Text & " was found within the file")


                reader.ReadLine()
                strLast = reader.ReadLine()
                strNumber = reader.ReadLine()

                lstCustomers.Items.Add("Customer's Last Name: " & strLast)
                lstCustomers.Items.Add("Customer's number: " & strNumber)


            Else
                MessageBox.Show("Customer was NOT found within the file")
            End If

        Catch ex As Exception
            MessageBox.Show("Error")

        End Try
    End Sub
It's able to find the text, but I guess I don't know/understand how to display it properly.

Last edited by SuperDav21; 10-24-2011 at 10:53 PM.
Reply With Quote
  #2  
Old 04-07-2012, 05:28 PM
NFITC1 NFITC1 is offline
Centurion
 
Join Date: Apr 2006
Posts: 102
Default

The problem is your reader. It doesn't know where your IndexOf function found the match in the string you already instanciated(sp) so it's going to read the first line from the file then the next TWO lines will be stored and displayed. If you want to use your reader then you should readLine in a loop until you either find a match or run out of lines to read. You should parse each line as you read it and display the results based on that.
I can't atest to the syntax of this, but:

Code:
Dim line as String

Try
     reader = File.OpenText( "Customer Accounts.txt" )

     While Not reader.EndOfStream
          line = reader.ReadLine()
          If line.IndexOf( strLast ) > -1 OR line.IndexOf( strNumber ) > -1 then
               <split the line for number and name and display that>
          EndIf
     Loop
Something like that.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Searching a text file for a string
Searching a text file for a string
Searching a text file for a string Searching a text file for a string
Searching a text file for a string
Searching a text file for a string
Searching a text file for a string Searching a text file for a string Searching a text file for a string Searching a text file for a string Searching a text file for a string Searching a text file for a string Searching a text file for a string
Searching a text file for a string
Searching a text file for a string
 
Searching a text file for a string
Searching a text file for a string
 
-->