Question Reading file, split text and write to text boxes

d1973z28

New member
Joined
Jun 24, 2010
Messages
3
Programming Experience
Beginner
Hi. I'm trying to read a text file that contains info like this:

ACX-101-011 , J2168
BTXR-130A-013, D6733
AJ4-233-614, T8211

I want to split each line at the comma and write the left side to a textbox and the the right side to another textbox. I'm close, with the code below, but I can only post results from the first line in the file. How do I loop this and append the text results in each of the textboxes. I am admittedly a VB.NET.NOOB, and I'm pulling my hair out! Any help would be great. Thanks.

VB.NET:
Dim TempFile As String
	TempFile = "temp.txt"
	Dim sw As StreamWriter

        If File.Exists(TempFile) = False Then

            sw = File.CreateText(TempFile)
            sw.Flush()
            sw.Close()

        End If

        sw = File.AppendText(TempFile)

        sw.WriteLine(TextBox4.Text)

        sw.Flush()
        sw.Close()

        Dim reader As New StreamReader(TempFile)

        Dim data As String()

        data = reader.ReadLine().Split(",")

        TextBox1.Text = Trim(data(0))
        TextBox2.Text = Trim(data(1))

        reader.Close()
 
To loop through the lines of a text file using a StreamReader:
VB.NET:
Dim line As String = myStreamReader.ReadLine()

Do Until line Is Nothing
    'Use line here.

    line = myStreamReader.ReadLine()
Loop
It's simpler without a StreamReader though:
VB.NET:
For Each line As String In IO.File.ReadAllLines("file path here")
    'Use line here.
Next
 
Man, I feel thick. I don't understand how to apply your suggestion. I'm really lost.

What I've come up with works, but only for the first line of the file. It seems so simple, and I'm frankly embarrassed to have my butt kicked over a few lines of code....noob or not.
 
Back
Top