Results 1 to 2 of 2

Thread: Reading from a text file [Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    127

    Reading from a text file [Resolved]

    I use the following code to write individual lines into a text file:

    Dim m_strTestFile As String = "c:\test.txt"
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim writer As New IO.StreamWriter(m_strTestFile)

    writer.Write(TextBox1.Text & vbCrLf)
    writer.Write(TextBox2.Text & vbCrLf)
    writer.Write(TextBox3.Text & vbCrLf)
    writer.Write(TextBox4.Text & vbCrLf)
    writer.Flush()
    writer.Close()

    End Sub

    The test.text file looks like this with the text that has been typed in the boxes.

    Text Box Line 1
    Text Box Line 2
    Text Box Line 3
    Text Box Line 4

    If say the boxes were reset and did not have text in them any longer, how can I use the IO.StreamReader to recall the individual lines back into the text boxes they were written in originally with another button click? Would it be easier to use an xml format and if so could someone give an example of both read and write to get me started in that direction?

    Thanks in advance
    Last edited by teamdad; Jun 13th, 2004 at 08:15 PM.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    A basic way to do it is this :
    VB Code:
    1. Private Sub readTxt(ByVal path As String)
    2.         Dim sr As New IO.StreamReader(path)
    3.  
    4.         Me.textbox1.text = sr.ReadLine
    5.         Me.textbox2.text = sr.ReadLine
    6.         Me.textbox3.text = sr.ReadLine
    7.         Me.textbox4.text = sr.ReadLine
    8.         sr.Close()
    9.  
    10.     End Sub

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