Text File to MULTIPLE Text boxes

I would like to open a text file with approx 30 lines of information,

Page 1 of 1

8 Replies - 2644 Views - Last Post: 01 November 2008 - 10:36 AM Rate Topic: -----

#1 XRedlinedX  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 01-November 08

Text File to MULTIPLE Text boxes

Post icon  Posted 01 November 2008 - 07:16 AM

I am creating a program for data storage, i have approx 30 text boxes that information is entered into. i can have it save all 30 or so text boxes into a text file into a single data per line format. i can also have the program open that text file, but when it recalls that data, it puts all of the 30 lines into the first text box only. how can i have it take each line of the text file and put it into each textbox, from where it came from? I'm new to this .net version of visual basic and its been over a decade since i did any real vb programming. here is the code that i have in my program buttons.

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim Save As New SaveFileDialog()
		Dim myStreamWriter As System.IO.StreamWriter

		Save.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*"
		Save.CheckPathExists = True
		Save.Title = "Save"
		Save.ShowDialog(Me)

		Try
			myStreamWriter = System.IO.File.AppendText(Save.FileName)
			myStreamWriter.WriteLine(TextBox1.Text)
			myStreamWriter.WriteLine(TextBox2.Text)
			myStreamWriter.WriteLine(TextBox3.Text)
			myStreamWriter.WriteLine(TextBox4.Text)
			myStreamWriter.WriteLine(RichTextBox1.Text)
			myStreamWriter.WriteLine(TextBox5.Text)
			myStreamWriter.WriteLine(TextBox6.Text)
			myStreamWriter.WriteLine(TextBox7.Text)
			myStreamWriter.WriteLine(TextBox8.Text)
			myStreamWriter.WriteLine(TextBox9.Text)
			myStreamWriter.WriteLine(TextBox10.Text)
			myStreamWriter.WriteLine(TextBox11.Text)
			myStreamWriter.WriteLine(TextBox12.Text)
			myStreamWriter.WriteLine(TextBox13.Text)
			myStreamWriter.WriteLine(TextBox14.Text)
			myStreamWriter.WriteLine(TextBox15.Text)
			myStreamWriter.WriteLine(TextBox16.Text)
			myStreamWriter.WriteLine(TextBox17.Text)
			myStreamWriter.WriteLine(TextBox18.Text)
			myStreamWriter.WriteLine(TextBox19.Text)
			myStreamWriter.WriteLine(TextBox20.Text)
			myStreamWriter.WriteLine(TextBox21.Text)
			myStreamWriter.WriteLine(TextBox22.Text)
			myStreamWriter.WriteLine(TextBox23.Text)
			myStreamWriter.WriteLine(TextBox24.Text)
			myStreamWriter.WriteLine(TextBox25.Text)
			myStreamWriter.WriteLine(TextBox26.Text)
			myStreamWriter.WriteLine(TextBox27.Text)
			myStreamWriter.WriteLine(TextBox28.Text)
			myStreamWriter.Flush()
		Catch ex As Exception
		End Try
	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Dim Open As New OpenFileDialog()
		Dim myStreamReader As System.IO.StreamReader

		Open.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*"
		Open.CheckFileExists = True
		Open.Title = "Open"
		Open.ShowDialog(Me)
		Try
			Open.OpenFile()
			myStreamReader = System.IO.File.OpenText(Open.FileName)
			TextBox1.Text = myStreamReader.ReadToEnd
			TextBox2.Text = myStreamReader.ReadToEnd
			TextBox3.Text = myStreamReader.ReadToEnd
			TextBox4.Text = myStreamReader.ReadToEnd
			RichTextBox1.Text = myStreamReader.ReadToEnd
			TextBox5.Text = myStreamReader.ReadToEnd
			TextBox6.Text = myStreamReader.ReadToEnd
			TextBox7.Text = myStreamReader.ReadToEnd
			TextBox8.Text = myStreamReader.ReadToEnd
			TextBox9.Text = myStreamReader.ReadToEnd
			TextBox10.Text = myStreamReader.ReadToEnd
			TextBox11.Text = myStreamReader.ReadToEnd
			TextBox12.Text = myStreamReader.ReadToEnd
			TextBox13.Text = myStreamReader.ReadToEnd
			TextBox14.Text = myStreamReader.ReadToEnd
			TextBox15.Text = myStreamReader.ReadToEnd
			TextBox16.Text = myStreamReader.ReadToEnd
			TextBox17.Text = myStreamReader.ReadToEnd
			TextBox18.Text = myStreamReader.ReadToEnd
			TextBox19.Text = myStreamReader.ReadToEnd
			TextBox20.Text = myStreamReader.ReadToEnd
			TextBox21.Text = myStreamReader.ReadToEnd
			TextBox22.Text = myStreamReader.ReadToEnd
			TextBox23.Text = myStreamReader.ReadToEnd
			TextBox24.Text = myStreamReader.ReadToEnd
			TextBox25.Text = myStreamReader.ReadToEnd
			TextBox26.Text = myStreamReader.ReadToEnd
			TextBox27.Text = myStreamReader.ReadToEnd
			TextBox28.Text = myStreamReader.ReadToEnd
		Catch ex As Exception

		End Try
	End Sub

	Private Sub TextBox17_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox17.TextChanged

	End Sub
End Class



Is This A Good Question/Topic? 1

Replies To: Text File to MULTIPLE Text boxes

#2 jjsaw5  Icon User is offline

  • I must break you
  • member icon

Reputation: 90
  • View blog
  • Posts: 3,060
  • Joined: 04-January 08

Re: Text File to MULTIPLE Text boxes

Posted 01 November 2008 - 09:06 AM

I will correct your post but just for future reference code tags are in closed in [] not {}





Thank you for at least trying to use them! :^: :D
Was This Post Helpful? 0
  • +
  • -

#3 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: Text File to MULTIPLE Text boxes

Posted 01 November 2008 - 09:15 AM

Use the ReadLine instead of ReadToEnd.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim Open As New OpenFileDialog()
        Dim myStreamReader As System.IO.StreamReader

        Open.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*"
        Open.CheckFileExists = True
        Open.Title = "Open"
        Open.ShowDialog(Me)
        Try
            Open.OpenFile()
            myStreamReader = System.IO.File.OpenText(Open.FileName)
            TextBox1.Text = myStreamReader.ReadLine
            TextBox2.Text = myStreamReader.ReadLine
            TextBox3.Text = myStreamReader.ReadLine
            TextBox4.Text = myStreamReader.ReadLine
            RichTextBox1.Text = myStreamReader.ReadLine
            TextBox5.Text = myStreamReader.ReadLine
            TextBox6.Text = myStreamReader.ReadLine
            TextBox7.Text = myStreamReader.ReadLine
            TextBox8.Text = myStreamReader.ReadLine
            TextBox9.Text = myStreamReader.ReadLine
            TextBox10.Text = myStreamReader.ReadLine
            TextBox11.Text = myStreamReader.ReadLine
            TextBox12.Text = myStreamReader.ReadLine
            TextBox13.Text = myStreamReader.ReadLine
            TextBox14.Text = myStreamReader.ReadLine
            TextBox15.Text = myStreamReader.ReadLine
            TextBox16.Text = myStreamReader.ReadLine
            TextBox17.Text = myStreamReader.ReadLine
            TextBox18.Text = myStreamReader.ReadLine
            TextBox19.Text = myStreamReader.ReadLine
            TextBox20.Text = myStreamReader.ReadLine
            TextBox21.Text = myStreamReader.ReadLine
            TextBox22.Text = myStreamReader.ReadLine
            TextBox23.Text = myStreamReader.ReadLine
            TextBox24.Text = myStreamReader.ReadLine
            TextBox25.Text = myStreamReader.ReadLine
            TextBox26.Text = myStreamReader.ReadLine
            TextBox27.Text = myStreamReader.ReadLine
            TextBox28.Text = myStreamReader.ReadLine
        Catch ex As Exception

        End Try
    End Sub



Hope this helps

This post has been edited by gbertoli3: 01 November 2008 - 09:17 AM

Was This Post Helpful? 0
  • +
  • -

#4 dbasnett  Icon User is offline

  • D.I.C Addict

Reputation: 108
  • View blog
  • Posts: 601
  • Joined: 01-October 08

Re: Text File to MULTIPLE Text boxes

Posted 01 November 2008 - 09:33 AM

change myStreamReader.ReadToEnd
to myStreamReader.ReadLine
Was This Post Helpful? 0
  • +
  • -

#5 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: Text File to MULTIPLE Text boxes

Posted 01 November 2008 - 09:46 AM

Yes
Was This Post Helpful? 0
  • +
  • -

#6 dbasnett  Icon User is offline

  • D.I.C Addict

Reputation: 108
  • View blog
  • Posts: 601
  • Joined: 01-October 08

Re: Text File to MULTIPLE Text boxes

Posted 01 November 2008 - 10:08 AM

i was busy trying to figure out why this doesn't work as expected

		Dim s() As String = New String() {}
		Dim nl() As String = New String() {Environment.NewLine}
		'test.txt contains
		'test
		'random
		'foo
		'bar
		'fu
		'bar2
		Dim fs As New FileStream("C:\test.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None) 'CHANGE FILE NAME
		Dim myReader As New StreamReader(fs)
		'odd, very odd
		Dim oneline As String = myReader.ReadLine() 'one line = test
		fs.Seek(0, SeekOrigin.Begin)
		Dim Links = myReader.ReadToEnd 'links starts with random ??????
		s = Links.Split(nl, StringSplitOptions.RemoveEmptyEntries)
		Stop
		fs.Seek(0, SeekOrigin.Begin)
		Links = myReader.ReadToEnd 'links starts with test
		s = Links.Split(nl, StringSplitOptions.RemoveEmptyEntries)
		'end odd



i'll post this in new thread.

This post has been edited by dbasnett: 01 November 2008 - 10:33 AM

Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Text File to MULTIPLE Text boxes

Posted 01 November 2008 - 10:10 AM

I would check to make sure you're not at the end of the file, possible inside a while loop using the Peek Method of the StreamReader. Also, you have an empty catch block, essentially swallowing any error that may occur, real bad design practice (just my 2)

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim Open As New OpenFileDialog()
    Dim myStreamReader As System.IO.StreamReader

    Open.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*"
    Open.CheckFileExists = True
    Open.Title = "Open"
    Open.ShowDialog(Me)
    Try
        Open.OpenFile()
        myStreamReader = System.IO.File.OpenText(Open.FileName)
        While Not myStreamReader.Peek() = -1
        	TextBox1.Text = myStreamReader.ReadLine
            TextBox2.Text = myStreamReader.ReadLine
            TextBox3.Text = myStreamReader.ReadLine
            TextBox4.Text = myStreamReader.ReadLine
            RichTextBox1.Text = myStreamReader.ReadLine
            TextBox5.Text = myStreamReader.ReadLine
            TextBox6.Text = myStreamReader.ReadLine
            TextBox7.Text = myStreamReader.ReadLine
            TextBox8.Text = myStreamReader.ReadLine
            TextBox9.Text = myStreamReader.ReadLine
            TextBox10.Text = myStreamReader.ReadLine
            TextBox11.Text = myStreamReader.ReadLine
            TextBox12.Text = myStreamReader.ReadLine
            TextBox13.Text = myStreamReader.ReadLine
            TextBox14.Text = myStreamReader.ReadLine
            TextBox15.Text = myStreamReader.ReadLine
            TextBox16.Text = myStreamReader.ReadLine
            TextBox17.Text = myStreamReader.ReadLine
            TextBox18.Text = myStreamReader.ReadLine
            TextBox19.Text = myStreamReader.ReadLine
            TextBox20.Text = myStreamReader.ReadLine
            TextBox21.Text = myStreamReader.ReadLine
            TextBox22.Text = myStreamReader.ReadLine
            TextBox23.Text = myStreamReader.ReadLine
            TextBox24.Text = myStreamReader.ReadLine
            TextBox25.Text = myStreamReader.ReadLine
            TextBox26.Text = myStreamReader.ReadLine
            TextBox27.Text = myStreamReader.ReadLine
            TextBox28.Text = myStreamReader.ReadLine
        End While
        
    Catch ex As Exception

    End Try
End Sub


Was This Post Helpful? 0
  • +
  • -

#8 XRedlinedX  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 01-November 08

Re: Text File to MULTIPLE Text boxes

Posted 01 November 2008 - 10:29 AM

View Postgbertoli3, on 1 Nov, 2008 - 09:15 AM, said:

Use the ReadLine instead of ReadToEnd.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim Open As New OpenFileDialog()
        Dim myStreamReader As System.IO.StreamReader

        Open.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*"
        Open.CheckFileExists = True
        Open.Title = "Open"
        Open.ShowDialog(Me)
        Try
            Open.OpenFile()
            myStreamReader = System.IO.File.OpenText(Open.FileName)
            TextBox1.Text = myStreamReader.ReadLine
            TextBox2.Text = myStreamReader.ReadLine
            TextBox3.Text = myStreamReader.ReadLine
            TextBox4.Text = myStreamReader.ReadLine
            RichTextBox1.Text = myStreamReader.ReadLine
            TextBox5.Text = myStreamReader.ReadLine
            TextBox6.Text = myStreamReader.ReadLine
            TextBox7.Text = myStreamReader.ReadLine
            TextBox8.Text = myStreamReader.ReadLine
            TextBox9.Text = myStreamReader.ReadLine
            TextBox10.Text = myStreamReader.ReadLine
            TextBox11.Text = myStreamReader.ReadLine
            TextBox12.Text = myStreamReader.ReadLine
            TextBox13.Text = myStreamReader.ReadLine
            TextBox14.Text = myStreamReader.ReadLine
            TextBox15.Text = myStreamReader.ReadLine
            TextBox16.Text = myStreamReader.ReadLine
            TextBox17.Text = myStreamReader.ReadLine
            TextBox18.Text = myStreamReader.ReadLine
            TextBox19.Text = myStreamReader.ReadLine
            TextBox20.Text = myStreamReader.ReadLine
            TextBox21.Text = myStreamReader.ReadLine
            TextBox22.Text = myStreamReader.ReadLine
            TextBox23.Text = myStreamReader.ReadLine
            TextBox24.Text = myStreamReader.ReadLine
            TextBox25.Text = myStreamReader.ReadLine
            TextBox26.Text = myStreamReader.ReadLine
            TextBox27.Text = myStreamReader.ReadLine
            TextBox28.Text = myStreamReader.ReadLine
        Catch ex As Exception

        End Try
    End Sub



Hope this helps

Thank you all very much for your input, i dont know how i missed the Readline vs the readtoline, but i guess thats what you get for writing at 2am. Thank you again.
Was This Post Helpful? 0
  • +
  • -

#9 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: Text File to MULTIPLE Text boxes

Posted 01 November 2008 - 10:36 AM

Glad I could Help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1