Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a certain line number to a text box

Status
Not open for further replies.

Yoxy99

MIS
Jul 28, 2007
23
US
Hi, I have the following code that reads a text file into an array and then displays it into a textbox. The contents of my text file are :

General
Hey,There

My question is how can I get streamreader to ignore the first line of the file (General) and just read the second line of the file (Hey,There) because I do not need to display "General" in my textbox but I have to keep the text file in this format.

Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Dim sr As New IO.StreamReader(txtLoadPath.Text)
Dim tempArray()

While Not sr.EndOfStream
tempArray = Split(sr.ReadLine, ",")

txtPath.Text = (tempArray(0))


End While
sr.Close()
End Sub
 
you have your answer in your code. this sort of thing is common.

You just "burn" the first row.

run the read once before entering your loop.

If your text box is Multiline then you should be able to request a specific line.

-Sometimes the answer to your question is the hack that works
 

I am very new to VB.NET, but what about this:
Code:
    Private Sub btnLoad_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles btnLoad.Click

        Dim sr As New IO.StreamReader(txtLoadPath.Text)
        Dim strTextLine As String = ""
        Dim i As Integer = 0

        While Not sr.EndOfStream
            i += 1
            strTextLine = sr.ReadLine
            If i > 1 Then txtPath.Text = strTextLine
        End While
        sr.Close()
    End Sub

Have fun.

---- Andy
 
Andy,

That would work, but you would need a +=
In addition you are adding a logical comparision for every line when you can only have that condition once.

Code:
        sr.ReadLine
        While Not sr.EndOfStream
            strTextLine = sr.ReadLine
            txtPath.Text += vbCrLf + strTextLine
        End While
        sr.Close()

-Sometimes the answer to your question is the hack that works
 
Thanks for the help, I figured that like Andys code has it everytime you add .readline it reads that specific so if I just call that before I need to read a line it will get me to the correct place.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top