Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Reading a two line text file into seperate textbox

Reading a two line text file into seperate textbox

Scheduled Pinned Locked Moved Visual Basic
tutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    johnjsm
    wrote on last edited by
    #1

    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

    K 1 Reply Last reply
    0
    • J johnjsm

      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

      K Offline
      K Offline
      Kschuler
      wrote on last edited by
      #2

      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
      
      J 2 Replies Last reply
      0
      • K Kschuler

        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
        
        J Offline
        J Offline
        johnjsm
        wrote on last edited by
        #3

        Thanks you kindly for your help.

        1 Reply Last reply
        0
        • K Kschuler

          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
          
          J Offline
          J Offline
          johnjsm
          wrote on last edited by
          #4

          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.

          K 1 Reply Last reply
          0
          • J johnjsm

            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.

            K Offline
            K Offline
            Kschuler
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups