Search code examples
vb.netstreamreader

StreamReader Only Reads and Outputs Last Line From MultiLine, Spaced Text File


I'm trying to read values from a text file and input them into an array from where I can assign them to text boxes. My text file has the first line as a title name (string/char) and all subsequent lines contain numbers:

picture of text

There are multiple lines and each value is split by a white-space. My current code is:

If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
     Dim openreader As System.IO.StreamReader = New System.IO.StreamReader(openFileDialog1.FileName)
    Try

        While Not openreader.EndOfStream
            Dim currentline As String
            currentline = openreader.ReadLine()
            currentline = currentline.Trim(" "c)
            Dim inputparts() As String = currentline.Split(" "c)
            TextBox1.Text = inputparts(0)
            TextBox2.Text = inputparts(1) 'This gives out of bounds error
            TextBox3.Text = inputparts(2) 'This gives out of bounds error
            TextBox4.Text = inputparts(3) 'This gives out of bounds error
        End While

    Catch Ex As Exception
        MessageBox.Show("The file could not be read. The original error is: " & Ex.Message)
    End Try
    openreader.Close()
    End If

The issue with this is that the array inputparts has an out of bounds error for anything higher than inputparts(0) and inputparts(0), which is the only element recorded, is always the very last number of the last line. I did not want to define the dimensions of inputparts() since I was my input file to have the freedom to have a range of different values.

Why is the array not recording any values other than the final one - is it because my currentline ends up being the last line - and how can I fix this? Any help would be appreciated!


Solution

  • One way to put the parts which come from the split into textboxes would be to have references to the textboxes in an array and set them from the array of items from the line.

    With Math.Min we can be sure that if there are not enough items on the line then we don't try to set the text to something which doesn't exist.

    Using openreader As StreamReader = New StreamReader(openFileDialog1.FileName)
        Dim tb = {TextBox1, TextBox2, TextBox3, TextBox4}
    
        Try
            While Not openreader.EndOfStream
                Dim currentline As String
                currentline = openreader.ReadLine()
                currentline = currentline.Trim(" "c)
                Dim inputparts() As String = currentline.Split(" "c)
    
                For i = 0 To Math.Min(tb.Length, inputparts.Length)
                    tb(i).Text = inputparts(i)
                Next
    
            End While
    
        Catch ex As Exception
            MessageBox.Show("The file could not be read. The original error is: " & ex.Message)
        End Try
    
    End Using
    

    I used the Using statement because it makes sure that the file is closed even if an exception occurs.

    If you add Imports System.IO at the very top of the code, you don't have to keep typing it in things like System.IO.StreamReader.