[RESOLVED] [2005] How to read lines from a text file for use in a text box
Hey guys, first off I have been researching how to do this all week and cannot pull it off in my code. I want a text file to be read line by line so I can use the data of the lines in a text boxes.
So far I've got this which reads the lines of the text file:
Code:
Dim sr As StreamReader = New StreamReader("D:\Test.txt")
Dim line As String = Nothing
Dim linecount As Integer = 0
Do
line = sr.ReadLine()
If Not line = Nothing Then
linecount += 1
End If
Loop Until line Is Nothing
But I dont know how to retreive the read lines. A solution would be great, thanks.:wave:
Re: [2005] How to read lines from a text file for use in a text box
2 Quotes:
Quote:
Dim line As String = Nothing
Quote:
line = sr.ReadLine()
All you have to do is set the line variable to your textbox controls text property.
Issue solved, rate me!! :bigyello:
LOL
Also I would make your do into:
Code:
Do While Not sr.EndOfStream
'Put your code in here.....
'will detect end of stream/file for you
Loop
Good Luck!! :wave:
D
Re: [2005] How to read lines from a text file for use in a text box
when you assign the variable Line then you have the information you want.
I would also change the way you loop:
vb Code:
Dim myLine As String = String.Empty
Do Until sr.Peek <> - 1 'loop until no more to read
line = sr.readline 'variable line has the value
lineCount += 1 'add counter
me.textbox1.text = myLine 'assign the variable value to a textbox
Loop
edit I'm too slow again
Re: [2005] How to read lines from a text file for use in a text box
Thanks for the quick replys but I still have a problem. I want to specify which line is displayed in the text box in the code.
e.g
txtRead.Text = line(line4) - to read and copy the info on line 4
Here is my current working code:
Code:
Public Class Form1
Dim sr As IO.StreamReader = New IO.StreamReader(Application.StartupPath & "\new.txt")
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim line As String = Nothing
Dim linecount As Integer = 0
Do While Not sr.EndOfStream
line = sr.ReadLine()
If Not line = Nothing Then
linecount += 1
End If
Loop
txtRead.Text = line
End Sub
End Class
Thanks.
Re: [2005] How to read lines from a text file for use in a text box
Then when it gets to the line you want you simply grab it:
Code:
Do While Not sr.EndOfStream
line = sr.ReadLine()
If Not line = Nothing Then
linecount += 1
End If
If linecount = 4 then
Textbox1.Text = line
Exit Do
End If
Loop
Re: [2005] How to read lines from a text file for use in a text box
Yes, thankyou all, it was so obvious. Good work guys. :wave:
Re: [2005] How to read lines from a text file for use in a text box
If you problem is fixed then please remember to mark it resolved under Thread Tools.
Thanks and happy coding!! :wave:
D
Re: [2005] How to read lines from a text file for use in a text box
As an alternative which you might want to consider you could use this:
vb Code:
Dim myStr() As String = IO.File.ReadAllLines("D:\myFile.txt", System.Text.Encoding.UTF8)
Me.TextBox1.Text = myStr(0) 'assign the correct index value to the text, 0 = line1 in the text file and so on...