我想创建一个应用程序,它将从文本文件中读取特定行并将其显示在文本框中。该行将根据我将进行的列表框选择的数量进行选择。
这是代码:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To Me.CheckedListBox1.CheckedIndices.Count - 1
Me.CheckedListBox1.SetItemChecked(Me.CheckedListBox1.CheckedIndices(0),False)
Next i
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If CheckedListBox1.CheckedItems.Count <> 0 Then
Dim reader As New System.IO.StreamReader(CurDir() & "\" & "READ.txt")
Dim x As Integer
Dim s As String = ""
For x = 0 To CheckedListBox1.CheckedItems.Count - 1
s = s & "Answer " & (x + 1).ToString & ") " & CheckedListBox1.CheckedItems(x).ToString & ControlChars.CrLf & reader.ReadLine() & ControlChars.CrLf & ControlChars.CrLf
Next x
Answer.Text = (s)
Else
MessageBox.Show("Please select questions.", "Error", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Return
End If
End Sub
End Class
所以,让我说'检查'选中列表框中的第一,第二和第五项,我希望它从文本文件中读取第一行,第二行和第五行文本,并在文本框中显示它们。
当前代码只是按顺序读取第1,2,3(...)行,无论我检查了什么项目。
答案 0 :(得分:0)
每次从文件中读取一行以跟踪您正在阅读的行时增加一个计数器,并在行号与所选行号匹配时仅在文本框中添加一行。