How to use Streamreader to read different lines of

2019-08-30 11:40发布

问题:

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:

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)