I have a test.txt file that contains:

1/15/2011; somedescription ; Joe Blow ; $50
1/18/2011; somedescription ; John Doe ; $30
2/1/2011; somedescription; Joe Blow; $90

I want to be able to pull all rows of data associated with Joe Blow if a users selects Joe Blow from a combobox. In testing this out I added the code to a button. The button will add all lines but if I un-comment the two lines it does nothing.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim txtFile As String = "test.txt"

        If File.Exists(txtFile) Then
            Using sr As New StreamReader(txtFile)
                While Not sr.EndOfStream
                    Dim lineText As String() = sr.ReadLine().Split(";"c)

                    'If InStr(txtFile, "Joe Blow") <> -1 Then
                    ListView1.Items.Add(New ListViewItem(lineText))
                    'End If

                End While
            End Using
        Else
            MsgBox("Text file not found!")
        End If
    End Sub

I tried another way and the following will add all rows of data that have Joe Blow but the lines are written to the first column only.

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim txtFile As String = "test.txt"
        Dim lineText As String = Nothing

        If File.Exists(txtFile) Then
            Using sr As New StreamReader(txtFile)
                While Not sr.EndOfStream
                    'Dim lineText As String() = sr.ReadLine().Split(";"c)
                    lineText = sr.ReadLine()
                    Dim newLine() As String = lineText.Split(";"c)

                    If InStr(lineText, "Joe Blow") <> 0 Then
                        ListView1.Items.Add(New ListViewItem(lineText))
                    End If

                End While
            End Using
        Else
            MsgBox("Text file not found!")
        End If
    End Sub

Another interesting thing that I have found is if I wanted to remove lines that I didn't want and did a search, the search would only take place on the first column. I think my first solution would be the way to go. Any suggestions or help would be greatly appreciated.

Recommended Answers

All 3 Replies

Try this

Dim lines() As String = Split(My.Computer.FileSystem.ReadAllText(myfile), vbCrLf)
dim flt() as string = Filter(lines,"Joe Blow")

This will give you an array containing only the matching lines.

Try this

Dim lines() As String = Split(My.Computer.FileSystem.ReadAllText(myfile), vbCrLf)
dim flt() as string = Filter(lines,"Joe Blow")

This will give you an array containing only the matching lines.

Here is the code that I tried but only adds Joes name twice with no other values:

Dim myFile As String = "test.txt"
        Dim lines() As String = Split(My.Computer.FileSystem.ReadAllText(myFile), ";"c)
        Dim flt() As String = Filter(lines, "Joe Blow")
        ListView1.Items.Add(New ListViewItem(flt))

When I try a sample it works fine. Put in a break point after you apply the filter and check what data is in the filtered array. You have to add the items in a loop like

dim filtered() as String = Filter(lines,filtertext)

ListView1.Items.Clear

if not filtered is Nothing Then
    for each line as string in filtered
        ListView1.Items.Add(New ListViewItem(line))
    next
end if
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.