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.