如何使用Streamreader从.txt文件中读取不同的文本行到不同的ListBoxes?

时间:2014-04-22 02:11:04

标签: listbox streamreader streamwriter

如果已经被问过,请原谅我。我已经能够从.txt文件中找到大量关于写入ListBox的信息,而且这很简单。我似乎无法找到很多信息是将文本读回多个ListBox,准确地说是10 。在过去的12个小时里我一直在努力,我放弃了。我什么都做不了。要么将所有列表框行放入第一个testListBox,要么当我尝试使用if ... then语句时,不显示。我在这里尝试了很多东西。我的程序有10个列表框,但我只是尝试在前两个工作。如果我可以正确地进行那些,那么其余的应该很容易。这就是为什么有10个列表框被写入,只有2个被读取。这是我的编写代码和我的读代码:

   ' 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

我刚刚学习Visual Basic,这是我编程类的第一个学期。我正在使用Visual Basic 2013.我意识到我有很多需要学习的东西。我知道我仍然需要添加评论,什么不是,但我真的只想在继续讨论该程序的其他方面之前解决这个问题。

我试图在“testListBox.items”中添加一个前缀,这样可行,但是当我尝试在StreamReader部分使用任何类型的if ... then语句来指定它的位置时行应该去,列表框中没有任何内容。除此之外,我不知道如何在某个ListBox中写入某一行。有人可以帮帮我吗?我已经阅读了很多关于使用listview而不是listbox的内容,但是我花了很多时间来设置所有这些,我真的不想再做这么多,如果还有其他的话使这项工作的方式。比

编辑: 所以,我摆脱了逗号,现在唯一的事情是它将testListBox.items放在所有列表框中。那是因为我没有循环了,还是因为你提到的string.join缺乏?如果我将循环添加回混音,程序就会卡住。如何解释列表框中应该包含的内容?或者我应该从写部分中删除For i =... Then语句?

     ' 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 个答案:

答案 0 :(得分:0)

虽然我会选择以不同的方式编写此代码,但您确实非常接近您正在寻找的解决方案。用这段代码写出列表框时......

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

...一定要在它们之间写一行。这样,您就可以获得多个以逗号分隔的行,而不只是一行。

当您重读这些内容时,只需为每个以逗号分隔的行执行SR.ReadLine并使用String.Split将其拆分。 String.Split返回一个字符串数组,你用逗号传递它,因为它是你加入字符串时使用的。 (您还可以使用String.Join调用替换使用String.Join调用来编写这些行的循环。)

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

有关VB.NET和Split的示例,请参阅此页面。 http://www.dotnetperls.com/split-vbnet

EDIT 剩下要做的就是为每个列表框添加readline调用,如下所示:

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)