如何使用Streamreader将.txt文件中的不同文本行读取到不同的ListBoxes中? [英] How to use Streamreader to read different lines of text from .txt file into different ListBoxes?

查看:95
本文介绍了如何使用Streamreader将.txt文件中的不同文本行读取到不同的ListBoxes中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问我是否已经被问到了.我已经能够找到大量有关从.txt文件写入ListBox的信息,这非常容易.我似乎找不到太多信息的是,将文本读回到多个ListBoxes中,确切地说是10个.在过去的12个小时中,我一直在为此工作,但我放弃了.我什么都没做.要么所有列表框行都放入第一个testListBox中,要么当我尝试使用if ... then语句时不显示.我尝试了太多的事情,无法在此处列出.我的程序总共有10个列表框,但我一直只在尝试两个列表框.如果我能正确解决这些问题,那么其余的工作应该很容易.这就是为什么要写入10个列表框,而只读取2个列表框的原因.这是我的写代码和我的读代码:

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

我只是在学习Visual Basic,这是本编程课程的第一学期.我正在使用Visual Basic2013.我意识到我有很多东西要学习.我知道我仍然需要添加注释,而不必添加注释,但是我真的只想在继续该程序的其他方面之前克服这个特定的问题.

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.

例如,我尝试在"testListBox.items"上添加前缀,并且可以使用,但是当我尝试在StreamReader部分上使用任何类型的if ... then语句来指定前缀时,行应该走,列表框中什么都没有显示.除此之外,我不知道如何在特定的ListBox中写入特定的行.有人可以帮我吗?我已经阅读了很多有关使用列表视图而不是列表框的内容,但是我花了很多时间来设置所有这些内容,我真的不想再做那么多的事情,如果还有其他事情的话这项工作的方式.比

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

因此,我摆脱了逗号,现在唯一的事情是将testListBox.items放在所有列表框中.是因为我没有循环了,还是因为您提到的string.join的缺乏?如果我将循环添加回混音中,则程序将卡住.如何解释应该在哪个列表框中显示的内容?还是应该从写部分中删除For i =... Then语句?

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

推荐答案

尽管我选择以明显不同的方式编写此代码,但您确实接近所要查找的解决方案.当您用此代码写出列表框时...

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.

当您重新阅读这些内容时,只需对每个逗号分隔的行执行SR.ReadLine并使用String.Split对其进行拆分. String.Split返回一个字符串数组,并且您将其传递给逗号,因为这是您在连接字符串时所使用的. (您也可以用String.Join调用替换编写这些行的循环,以使代码更简洁.)

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)

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

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

编辑 剩下要做的唯一一件事就是为每个列表框添加readline调用,如下所示:

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)

这篇关于如何使用Streamreader将.txt文件中的不同文本行读取到不同的ListBoxes中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆