vb.net read file line by line and copy some part of the Line

5.5k Views Asked by At

I have this code to read the read the lines of a file (16file) and then show each line in a textbox (tWLine), one at a time but the code below shows all the contents of the file at once. However I want it such that after a line of the file is read, it is shown in the textbox (tWline) and then a button is clicked before the next line is read and shown in the textbox again till the last line in the 16file.

The second part will be to copy the words in the line that is read into textboxes based on the number of the word in the line (1st word into txt1, 2nd into txt2, etc) but I will be most grateful to at least get through with even the first part alone.

    `Dim fRdr As StreamReader

    fRdr = File.OpenText("C:\users\sk\16file.txt")

    'Loop through to read Lines
    Do Until fRdr.EndOfStream
        tWLine.Text = tWLine.Text & fRdr.ReadLine & vbCrLf
    Loop
    fRdr.Close()`

I would like the textbox contents to appear like the code below shows the messages one line at a time so that after each line has shown in the tWline textbox, some form of code will be performed before the next line is read and then the same thing will be done for the next line till the last line in the file is reached.

    `Private Sub bReadFA_Click(sender As Object, e As EventArgs) Handles bReadFA.Click
    For Each Line As String In File.ReadLines("C:\users\sk\16file.txt")
        MsgBox(line)
    Next
End Sub `
2

There are 2 best solutions below

7
On BEST ANSWER

You mean transfer each line of your file in textbox? try this

change Msgbox(line) to YourTextbox.Text = line

Try this one

Private Sub bReadFA_Click(sender As Object, e As EventArgs) Handles bReadFA.Click
For Each Line As String In File.ReadLines("C:\users\sk\16file.txt")
YourTextbox.Text = line
await Task.Delay(1000)//Await a second
Next
4
On

If Shadow Fiend's answer doesn't worked for you then you might not having multiple lines in your text file. If you mean sentence by saying line, then you better copy all the text in the file to a String variable and split it by means of '.' to get the string array. and then simply do what ever you want the the array.