0

Forgive me if this has already been asked. I've been able to find tons of info on writing to a ListBox from a .txt file, and that's supereasy. What I can't seem to find much info on is reading that text back into to multiple ListBoxes, 10 to be exact. I've been working on this for the last 12 hours and I give up. Nothing I do works. either all the listbox lines get put into the 1st testListBox, or when I try using an if...then statement, not show up. I've tried too many things to list here. My program has 10 listboxes all together, but I've only been trying to work on the 1st two. if I can get those going correctly, then the rest should be easy. That's why there are 10 listboxes being written and only 2 being read. Here is my write code and my read code:

   ' handles saveButton Click
   Public Sub saveButton_Click(sender As Object, e As EventArgs) Handles saveButton.Click

  SaveFileDialog1.InitialDirectory = "c:\users\jeremy\desktop"
  SaveFileDialog1.FileName = (nameOfCourseTextBox.Text)
  SaveFileDialog1.Filter = "Text File ONLY (*.txt)|*.txt"
  SaveFileDialog1.ShowDialog()

  Dim SW As New StreamWriter(SaveFileDialog1.FileName)
  Dim i As Integer

  SW.WriteLine(nameOfCourseTextBox.Text)
  SW.WriteLine(creditsTextBox.Text)
  SW.WriteLine(testTypeLabel.Text)
  SW.WriteLine(testWeightLabel.Text)
  SW.WriteLine(attendanceTypeLabel.Text)
  SW.WriteLine(attendanceWeightLabel.Text)
  SW.WriteLine(assignmentTypeLabel.Text)
  SW.WriteLine(assignmentWeightLabel.Text)
  SW.WriteLine(otherTypeLabel.Text)
  SW.WriteLine(otherWeightLabel.Text)
  SW.WriteLine(projectTypeLabel.Text)
  SW.WriteLine(projectWeightLabel.Text)
  SW.WriteLine(aTextBox.Text)
  SW.WriteLine(bTextBox.Text)
  SW.WriteLine(cTextBox.Text)
  SW.WriteLine(dTextBox.Text)
  SW.WriteLine(fTextBox.Text)
  For i = 0 To testListBox.Items.Count - 1
     SW.Write(testListBox.Items.Item(i) & ",")
  Next
  For i = 0 To gradeTestListBox.Items.Count - 1
     SW.Write(gradeTestListBox.Items.Item(i) & ",")
  Next
  For i = 0 To assignmentListBox.Items.Count - 1
     SW.WriteLine(assignmentListBox.Items.Item(i))
  Next
  For i = 0 To gradeAssignmentListBox.Items.Count - 1
     SW.WriteLine(gradeAssignmentListBox.Items.Item(i))
  Next
  For i = 0 To attendanceListBox.Items.Count - 1
     SW.WriteLine(attendanceListBox.Items.Item(i))
  Next
  For i = 0 To gradeAttendanceListBox.Items.Count - 1
     SW.WriteLine(gradeAttendanceListBox.Items.Item(i))
  Next
  For i = 0 To projectListBox.Items.Count - 1
     SW.WriteLine(projectListBox.Items.Item(i))
  Next
  For i = 0 To gradeProjectListBox.Items.Count - 1
     SW.WriteLine(gradeProjectListBox.Items.Item(i))
  Next
  For i = 0 To otherListBox.Items.Count - 1
     SW.WriteLine(otherListBox.Items.Item(i))
  Next
  For i = 0 To gradeOtherListBox.Items.Count - 1
     SW.WriteLine(gradeOtherListBox.Items.Item(i))
  Next
  SW.Close()

   End Sub ' saveButton_Click

   Public Sub openFileButton_Click(sender As Object, e As EventArgs) Handles     openFileButton.Click

  OpenFileDialog1.InitialDirectory = "c:\users\jeremy\desktop"
  OpenFileDialog1.Filter = "Text File ONLY (*txt)|*txt"
  OpenFileDialog1.ShowDialog()

  Dim SR As New StreamReader(OpenFileDialog1.FileName)

  nameOfCourseTextBox.Text = SR.ReadLine
  creditsTextBox.Text = SR.ReadLine
  testTypeLabel.Text = SR.ReadLine
  testWeightLabel.Text = SR.ReadLine
  attendanceTypeLabel.Text = SR.ReadLine
  attendanceWeightLabel.Text = SR.ReadLine
  assignmentTypeLabel.Text = SR.ReadLine
  assignmentWeightLabel.Text = SR.ReadLine
  otherTypeLabel.Text = SR.ReadLine
  otherWeightLabel.Text = SR.ReadLine
  projectTypeLabel.Text = SR.ReadLine
  projectWeightLabel.Text = SR.ReadLine
  aTextBox.Text = SR.ReadLine
  bTextBox.Text = SR.ReadLine
  cTextBox.Text = SR.ReadLine
  dTextBox.Text = SR.ReadLine
  fTextBox.Text = SR.ReadLine
  Do While (SR.Peek() > -1)
     testListBox.Items.Add(SR.ReadLine)
  Loop
  Do While (SR.Peek() > -1)
     gradeTestListBox.Items.Add(SR.ReadLine)
  Loop

I'm just learning Visual Basic, this is my first semester of this programming class. I'm using Visual Basic 2013. I realize I have a lot to learn. I know that I still need to add comments and what not, but I really just want to get past this particular problem before I continue on to other aspects of the program.

I've tried to add a prefix to the "testListBox.items", for example, and that works, but when I try to use any sort of an if...then statement on the StreamReader part to specify where that line should go, nothing shows up in the listbox. Aside from that, I have no idea how to get a certain line to be written in a certain ListBox. Can anyone help me out here, please? I've read a lot of stuff about using a listview instead of listbox, but I've spend so much time setting all this up, I really don't want to have to do so much of it over again, if there's any other way to make this work. Than

Edit: So, I got rid of the commas, the only thing now is that it's putting the testListBox.items in all the listboxes. Is that because I don't have the loop going anymore, or because of the lack of string.join that you mentioned? If I add the loop back into the mix, the program gets stuck. How is it to account for the what's supposed to be in what listbox? Or should I have gotten rid of the For i =... Then statement from the write section?

     ' handles saveButton Click
     Public Sub saveButton_Click(sender As Object, e As EventArgs) Handles       saveButton.Click

  SaveFileDialog1.InitialDirectory = "c:\users\jeremy\desktop"
  SaveFileDialog1.FileName = (nameOfCourseTextBox.Text)
  SaveFileDialog1.Filter = "Text File ONLY (*.txt)|*.txt"
  SaveFileDialog1.ShowDialog()

  Dim SW As New StreamWriter(SaveFileDialog1.FileName)
  Dim i As Integer

  SW.WriteLine(nameOfCourseTextBox.Text)
  SW.WriteLine(creditsTextBox.Text)
  SW.WriteLine(testTypeLabel.Text)
  SW.WriteLine(testWeightLabel.Text)
  SW.WriteLine(attendanceTypeLabel.Text)
  SW.WriteLine(attendanceWeightLabel.Text)
  SW.WriteLine(assignmentTypeLabel.Text)
  SW.WriteLine(assignmentWeightLabel.Text)
  SW.WriteLine(otherTypeLabel.Text)
  SW.WriteLine(otherWeightLabel.Text)
  SW.WriteLine(projectTypeLabel.Text)
  SW.WriteLine(projectWeightLabel.Text)
  SW.WriteLine(aTextBox.Text)
  SW.WriteLine(bTextBox.Text)
  SW.WriteLine(cTextBox.Text)
  SW.WriteLine(dTextBox.Text)
  SW.WriteLine(fTextBox.Text)
  For i = 0 To testListBox.Items.Count - 1
     SW.Write(testListBox.Items.Item(i) & ",")
  Next i
  SW.WriteLine() ' creats new line
  For i = 0 To gradeTestListBox.Items.Count - 1
     SW.Write(gradeTestListBox.Items.Item(i) & ",")
  Next i
  SW.WriteLine() ' creats new line
  For i = 0 To assignmentListBox.Items.Count - 1
     SW.Write(assignmentListBox.Items.Item(i))
  Next i
  SW.WriteLine() ' creats new line
  For i = 0 To gradeAssignmentListBox.Items.Count - 1
     SW.Write(gradeAssignmentListBox.Items.Item(i))
  Next
  SW.WriteLine() ' creats new line
  For i = 0 To attendanceListBox.Items.Count - 1
     SW.Write(attendanceListBox.Items.Item(i))
  Next
  SW.WriteLine() ' creats new line
  For i = 0 To gradeAttendanceListBox.Items.Count - 1
     SW.Write(gradeAttendanceListBox.Items.Item(i))
  Next
  SW.WriteLine() ' creats new line
  For i = 0 To projectListBox.Items.Count - 1
     SW.Write(projectListBox.Items.Item(i))
  Next
  SW.WriteLine() ' creats new line
  For i = 0 To gradeProjectListBox.Items.Count - 1
     SW.Write(gradeProjectListBox.Items.Item(i))
  Next
  SW.WriteLine() ' creats new line
  For i = 0 To otherListBox.Items.Count - 1
     SW.Write(otherListBox.Items.Item(i))
  Next
  SW.WriteLine() ' creats new line
  For i = 0 To gradeOtherListBox.Items.Count - 1
     SW.Write(gradeOtherListBox.Items.Item(i))
  Next
  SW.WriteLine() ' creats new line
  SW.Close()

    End Sub ' saveButton_Click

     Public Sub openFileButton_Click(sender As Object, e As EventArgs) Handles       openFileButton.Click

  OpenFileDialog1.InitialDirectory = "c:\users\jeremy\desktop"
  OpenFileDialog1.Filter = "Text File ONLY (*txt)|*txt"
  OpenFileDialog1.ShowDialog()

  Dim SR As New StreamReader(OpenFileDialog1.FileName)

  nameOfCourseTextBox.Text = SR.ReadLine
  creditsTextBox.Text = SR.ReadLine
  testTypeLabel.Text = SR.ReadLine
  testWeightLabel.Text = SR.ReadLine
  attendanceTypeLabel.Text = SR.ReadLine
  attendanceWeightLabel.Text = SR.ReadLine
  assignmentTypeLabel.Text = SR.ReadLine
  assignmentWeightLabel.Text = SR.ReadLine
  otherTypeLabel.Text = SR.ReadLine
  otherWeightLabel.Text = SR.ReadLine
  projectTypeLabel.Text = SR.ReadLine
  projectWeightLabel.Text = SR.ReadLine
  aTextBox.Text = SR.ReadLine
  bTextBox.Text = SR.ReadLine
  cTextBox.Text = SR.ReadLine
  dTextBox.Text = SR.ReadLine
  fTextBox.Text = SR.ReadLine

  Dim S As String() = SR.ReadLine.Split(New Char() {","c})
  testListBox.Items.AddRange(S)
  gradeTestListBox.Items.AddRange(S)
  assignmentListBox.Items.AddRange(S)
  gradeAssignmentListBox.Items.AddRange(S)
  SR.Close()

     End Sub

1 Answers1

0

Although I would choose to write this code significantly differently, you're really close to the solution you're looking for. When you write out the listboxes with this code...

For i = 0 To testListBox.Items.Count - 1
     SW.Write(testListBox.Items.Item(i) & ",")
Next
SW.WriteLine(); // <<-- Create a new line...
For i = 0 To gradeTestListBox.Items.Count - 1
     SW.Write(gradeTestListBox.Items.Item(i) & ",")
Next

...be sure to write a line between them. That way, you've got multiple comma-delimited lines instead of just one long one.

When you read these back in, just do a SR.ReadLine for each comma-delimited line and split it with String.Split. String.Split returns an array of strings and you pass it a comma because that's what you used when joining the strings. (You could also replace your loops that write these lines with String.Join calls to make your code cleaner.)

Dim s As String() = SW.ReadLine().Split(New Char() {","c})
testListBox.Items.AddRange(s)

See this page for examples of VB.NET and Split. http://www.dotnetperls.com/split-vbnet

EDIT The only thing left to do is add readline calls for each of your listboxes, like this:

Dim S As String() = SR.ReadLine.Split(New Char() {","c})
testListBox.Items.AddRange(S)
S = SR.ReadLine().Split(New Char() {","c})
gradeTestListBox.Items.AddRange(S)
S = SR.ReadLine().Split(New Char() {","c})
assignmentListBox.Items.AddRange(S)
S = SR.ReadLine().Split(New Char() {","c})
gradeAssignmentListBox.Items.AddRange(S)
Markus
  • 761
  • 3
  • 6
  • Ok, so streamreader/writer is out of the scope of my class. This program is a project, and while I'm satisfied with it and it meets all requirements, if I can't save it and open it, then it's pretty worthless to me personally. That's why I've been trying to learn this "extra" stuff. That being said, I barely have a grasp on streamreader/writer as it is, so the String.split and String.join is above my head. Can you show me an example of that code? I've been trying to figure it out, but it's just not syncing in. If not, I'll continue to try and figure it out. Thank you for responding. –  Apr 22 '14 at 02:48
  • I've added more sample code to the answer to explain each recommendation. – Markus Apr 22 '14 at 03:00
  • Ok, I've tried to incorporate your suggestions, but it's not working. I've edited my original post and added my new code along with explanations of what's not working. I've got to be almost there, hopefully. What am I doing wrong here? –  Apr 22 '14 at 14:51
  • Yep, you're very close. The code executes in the order that you see it. So, you must read the data in the same order that you wrote it, which means the line that does the read and Split is way too early. Because you are writing the testListBox items after fTextbox, you must *read* the items after you read the content for fTextbox. Move the following line: Dim S As String() = SR.ReadLine.Split(New Char() {","c}) ...down to just before this line: testListBox.Items.AddRange(S) Also, you'll need to add the same type of calls for each of your listboxes. – Markus Apr 22 '14 at 15:10
  • Also, you didn't need to change the other WriteLine calls. They were fine as they were originally, like this: SW.WriteLine(nameOfCourseTextBox.Text) SW.WriteLine(creditsTextBox.Text) SW.WriteLine(testTypeLabel.Text) SW.WriteLine(testWeightLabel.Text) SW.WriteLine(attendanceTypeLabel.Text) SW.WriteLine(attendanceWeightLabel.Text) SW.WriteLine(assignmentTypeLabel.Text) – Markus Apr 22 '14 at 15:15
  • I've edited my last edit, with new info and what will hopefully be the last issue. –  Apr 22 '14 at 16:17
  • You got it! The only thing you need to do is add additional readline calls for each listbox. See the edit that I added to the bottom of my answer. – Markus Apr 22 '14 at 16:22
  • That worked perfectly. Now, I think what will hopefully be my last problem is how to convert these listbox strings back to integers so that I can use them to calculate totals again. I've tried using `CInt` and `ToInt32` and several other things, but none of them work. I think when I'm done with project I will never use `ListBox` again. –  Apr 22 '14 at 18:19
  • We're getting a little long on the comments on this post. If you're still having issues with the conversion, post that as a separate question and it'll get answered. Be sure to include examples of the strings from your listboxes, so that the community can see what they look like. – Markus Apr 22 '14 at 22:10