Results 1 to 9 of 9

Thread: [RESOLVED] Help! Vb.net Reading a text file line by line

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2011
    Posts
    70

    Resolved [RESOLVED] Help! Vb.net Reading a text file line by line

    ok so i got vb to read the text file but it reads the whole thing and displays it on a text line,
    how can i make it so it reads it like 1 line at a time after every click,
    so if my text file is like this
    hello
    goodbye

    then i dont want it to display both, i want it to display hello and if i click a button again then it displays goodbye,

    Thanks in advance

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Help! Vb.net Reading a text file line by line

    here's how. on load it reads your text file into a string array, then displays the lines 1 by 1 as you click the button:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim lines() As String
    4.     Dim index As Integer = 0
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         lines = IO.File.ReadAllLines("filename.txt")
    8.     End Sub
    9.  
    10.     Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
    11.         TextBox1.Text &= lines(index) & Environment.NewLine
    12.         index += 1
    13.         If index > lines.GetUpperBound(0) Then button1.Enabled = False
    14.     End Sub
    15.  
    16. End Class

  3. #3
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Help! Vb.net Reading a text file line by line

    vb Code:
    1. '
    2. Public Class Form1
    3.  
    4.     Private sr As IO.StreamReader
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         Debug.Assert(Not IsNothing(sr))
    8.  
    9.         ' checking whether we're not at the end yet
    10.         If Not (sr.EndOfStream) Then
    11.             ' Read the next line and display in in the texbox
    12.             TextBox1.Text = sr.ReadLine()
    13.         Else
    14.             MessageBox.Show("Reached the end of file")
    15.         End If
    16.     End Sub
    17.  
    18.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    19.         sr.Close()
    20.         sr.Dispose()
    21.     End Sub
    22.  
    23.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    24.         Using dlg As New OpenFileDialog With {.CheckFileExists = True}
    25.             Dim ret As DialogResult = dlg.ShowDialog(Me)
    26.             If ret = Windows.Forms.DialogResult.OK Then
    27.                 Try
    28.                     ' Open file for reading
    29.                     sr = New IO.StreamReader(New IO.FileStream(dlg.FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read))
    30.                 Catch ex As Exception
    31.                     ' Catching possible exceptions
    32.                     MessageBox.Show(ex.Message)
    33.                 End Try
    34.             End If
    35.         End Using
    36.  
    37.     End Sub
    38. End Class

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2011
    Posts
    70

    Re: Help! Vb.net Reading a text file line by line

    Quote Originally Posted by .paul. View Post
    here's how. on load it reads your text file into a string array, then displays the lines 1 by 1 as you click the button:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim lines() As String
    4.     Dim index As Integer = 0
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         lines = IO.File.ReadAllLines("filename.txt")
    8.     End Sub
    9.  
    10.     Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
    11.         TextBox1.Text &= lines(index) & Environment.NewLine
    12.         index += 1
    13.         If index > lines.GetUpperBound(0) Then button1.Enabled = False
    14.     End Sub
    15.  
    16. End Class

    thank you but it did not work as i intended it did not display the second part it only displayed the first part.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2011
    Posts
    70

    Re: Help! Vb.net Reading a text file line by line

    Quote Originally Posted by cicatrix View Post
    vb Code:
    1. '
    2. Public Class Form1
    3.  
    4.     Private sr As IO.StreamReader
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         Debug.Assert(Not IsNothing(sr))
    8.  
    9.         ' checking whether we're not at the end yet
    10.         If Not (sr.EndOfStream) Then
    11.             ' Read the next line and display in in the texbox
    12.             TextBox1.Text = sr.ReadLine()
    13.         Else
    14.             MessageBox.Show("Reached the end of file")
    15.         End If
    16.     End Sub
    17.  
    18.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    19.         sr.Close()
    20.         sr.Dispose()
    21.     End Sub
    22.  
    23.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    24.         Using dlg As New OpenFileDialog With {.CheckFileExists = True}
    25.             Dim ret As DialogResult = dlg.ShowDialog(Me)
    26.             If ret = Windows.Forms.DialogResult.OK Then
    27.                 Try
    28.                     ' Open file for reading
    29.                     sr = New IO.StreamReader(New IO.FileStream(dlg.FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read))
    30.                 Catch ex As Exception
    31.                     ' Catching possible exceptions
    32.                     MessageBox.Show(ex.Message)
    33.                 End Try
    34.             End If
    35.         End Using
    36.  
    37.     End Sub
    38. End Class
    Thank you but that did not work,my app crashed...

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Help! Vb.net Reading a text file line by line

    Quote Originally Posted by M0NKEYRASH View Post
    thank you but it did not work as i intended it did not display the second part it only displayed the first part.
    in your text file, does it look like this:

    hello
    goodbye
    ?

    with each line on a new line?

  7. #7

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2011
    Posts
    70

    Re: Help! Vb.net Reading a text file line by line

    Quote Originally Posted by .paul. View Post
    in your text file, does it look like this:



    ?

    with each line on a new line?

    yes my text file did look like that but what the app did was just display the first line over and over but it did not display it once it did it like this, hellohellohello
    everclick

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2011
    Posts
    70

    Re: Help! Vb.net Reading a text file line by line

    Quote Originally Posted by cicatrix View Post
    What do you mean 'crashed'? At which line?
    it did not crash that was my other app i was working on while i was waiting for an answer,silly me and thank you for helping i do like how it opens up the open page and how would i make it read two txt files ? i really do like the code ratings for both of you!

    EDIT: and how to do i name the OPEN so i can specify which txt to choose?
    Last edited by M0NKEYRASH; Jul 28th, 2011 at 05:57 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width