Reading a two line text file into seperate textbox
-
Hi, I am trying to read two different lines from a text file into two seperate textboxs ie. line one of text file goes into textbox one and line two goes into text box two. Does anyone know how to do this. Thanks
Here is one way:
'Declare a StreamReader object to read the text file line by line Dim sReader As New System.IO.StreamReader("C:\\MyTextFile.txt") Dim strLine As String = String.Empty 'Read first line of text file strLine = sReader.ReadLine If Not strLine Is Nothing Then 'Make sure text file had a first line txt1.Text = strLine 'Set first textbox strLine = sReader.ReadLine 'Read second line of text file If Not strLine Is Nothing Then 'Make sure text file had a second line txt2.Text = strLine 'Set second textbox End If End If sReader.Close() 'Close reader object to keep memory clean of garbage
-
Here is one way:
'Declare a StreamReader object to read the text file line by line Dim sReader As New System.IO.StreamReader("C:\\MyTextFile.txt") Dim strLine As String = String.Empty 'Read first line of text file strLine = sReader.ReadLine If Not strLine Is Nothing Then 'Make sure text file had a first line txt1.Text = strLine 'Set first textbox strLine = sReader.ReadLine 'Read second line of text file If Not strLine Is Nothing Then 'Make sure text file had a second line txt2.Text = strLine 'Set second textbox End If End If sReader.Close() 'Close reader object to keep memory clean of garbage
-
Here is one way:
'Declare a StreamReader object to read the text file line by line Dim sReader As New System.IO.StreamReader("C:\\MyTextFile.txt") Dim strLine As String = String.Empty 'Read first line of text file strLine = sReader.ReadLine If Not strLine Is Nothing Then 'Make sure text file had a first line txt1.Text = strLine 'Set first textbox strLine = sReader.ReadLine 'Read second line of text file If Not strLine Is Nothing Then 'Make sure text file had a second line txt2.Text = strLine 'Set second textbox End If End If sReader.Close() 'Close reader object to keep memory clean of garbage
-
Hi, I know its a long time since you posted this reply but I have one question. The text file I am reading the first line is blank. Will this code still work.
You may have to add another strLine = sReader.ReadLine to the code I gave you before. You will have to read in the first line (even if it is blank) but then just don't do anything with and read the second line right away. So in the spot where you read the first line of text, it would look more like this:
'Read first line of text file strLine = sReader.ReadLine 'Don't do anything with the first line because it is blank 'Read the second line strLine = sReader.ReadLine
Hope this helps.