I have this Private Sub that reads a text file line by line and outputs to a textbox, I want to display the amount of lines read as it goes but when I run the lbl_LinesRead.Text only gets updated once the file read completes.
What am I doing wrong?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles BTN_StartProcess.Click
Dim linesRead As Integer
Dim fileReader As System.IO.StreamReader
Dim stringReader As String
linesRead = 0
fileReader = My.Computer.FileSystem.OpenTextFileReader(fileToRead)
Do
stringReader = fileReader.ReadLine()
TXT_Output.AppendText(stringReader & vbCrLf)
linesRead += 1
lbl_LinesRead.Text = linesRead.ToString
Loop Until stringReader Is Nothing
End Sub
Number to update on each line read
julianhaines is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
You need to force the label to redraw. You can do easily by adding
lbl_LinesRead.Refresh()
after lbl_LinesRead.Text = linesRead.ToString
Here is a .gif of the result (slowed down):