How to move text from a text file to a text box line by line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Monster55Me
    New Member
    • Dec 2014
    • 2

    How to move text from a text file to a text box line by line

    Alrighty, so I have a text file with a lot of different things in, but what I want is for when a button is pressed, only 1 row of text is shown. For example the text file says
    Potato - Potato
    I want the text box to say only Potato - Potato, nothing else in the file until the button is clicked again, which then will move onto showing the next line of text. If this is possible let me know, best way of informing me how it is done is by video as I find it the easiest, but text is fine, pictures will be very helpful also. Thanks.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Well, you need to read the file into memory and then use the StreamReader.Re adline Method to retrieve the next line and display it.

    Check out the link for an example of how to use the ReadLine method.

    -Frinny

    Comment

    • Monster55Me
      New Member
      • Dec 2014
      • 2

      #3
      Read the file into memory? Could you expand? I'm quite new to VB, sorry... Thanks for the help though :)

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        The example in the documentation demonstrates it pretty well.

        This example creates a new file that it then reads in (if the file exists before creation it deletes the file and creates a new one).
        Code:
        Imports System
        Imports System.IO
        Imports System.Text
        
        Public Class Test
        
            Public Shared Sub Main()
                Dim path As String = "c:\temp\MyTest.txt" 
        
                Try 
                    If File.Exists(path) Then
                        File.Delete(path)
                    End If 
        
                    Dim sw As StreamWriter = New StreamWriter(path)
                    sw.WriteLine("This")
                    sw.WriteLine("is some text")
                    sw.WriteLine("to test")
                    sw.WriteLine("Reading")
                    sw.Close()
        
                    Dim sr As StreamReader = New StreamReader(path)
        
                    Do While sr.Peek() >= 0
                        Console.WriteLine(sr.ReadLine())
                    Loop
                    sr.Close()
                Catch e As Exception
                    Console.WriteLine("The process failed: {0}", e.ToString())
                End Try 
            End Sub 
        End Class
        The Peek method is used to check if the stream that contains the file has another line available for reading. If the Peek method returns a line that has a lenght greater than 0 it reads it in and displays it on the screen.

        The try/catch block is there in case something goes wrong creating the file or reading the file.

        -Frinny

        Comment

        Working...