Vb.net 在VB中从文本文件的索引中读取一行

Vb.net 在VB中从文本文件的索引中读取一行,vb.net,readline,Vb.net,Readline,我正在创建一个测验应用程序,其中我已在单个文本文件中添加了所有问题和选项。但我不知道如何在单击“下一步”按钮时转到下一个问题,在单击“上一步”按钮时转到上一个问题。我当前面临的问题是读取文本文件中的特定行 这是“下一步”按钮的功能 Public Function Next_Ques() As Integer Label1.Text = file.ReadLine() RadioButton1.Text = file.ReadLine() Radi

我正在创建一个测验应用程序,其中我已在单个文本文件中添加了所有问题和选项。但我不知道如何在单击“下一步”按钮时转到下一个问题,在单击“上一步”按钮时转到上一个问题。我当前面临的问题是读取文本文件中的特定行

这是“下一步”按钮的功能

 Public Function Next_Ques() As Integer
        Label1.Text = file.ReadLine()
        RadioButton1.Text = file.ReadLine()
        RadioButton2.Text = file.ReadLine()
        RadioButton3.Text = file.ReadLine()
        RadioButton4.Text = file.ReadLine()
        ansKey = file.ReadLine()
        Return 0
    End Function

“下一步”按钮工作正常。但我不知道如何读取前面的行。

将所有行读取到字符串缓冲区并进行操作要比在文件流中搜索容易得多。使用以下命令:

Dim datas String() = File.ReadAllLines(filePath)
' for example:
Dim firstQuestion as String = datas(0)
Dim firstQuestionOption1 as String = datas(1)
Dim secondQuestion as String = datas(6) ' accord to your data structure
Dim secondQuestionOpetion2 as String = data(7)
然后,您可以自由地遍历它——当然,根据您的数据安排,取出测验问题和选项

之后,使用索引来记住哪个问题正在进行,当用户按下下一步/上一步按钮时,计算正确的开始索引,并从我们刚才阅读的数据中提取字符串


并在您的UI组件上显示它们,我相信这里所做的所有工作:)

将它们全部读取到字符串缓冲区并进行操作将比在文件流中搜索容易得多。使用以下命令:

Dim datas String() = File.ReadAllLines(filePath)
' for example:
Dim firstQuestion as String = datas(0)
Dim firstQuestionOption1 as String = datas(1)
Dim secondQuestion as String = datas(6) ' accord to your data structure
Dim secondQuestionOpetion2 as String = data(7)
然后,您可以自由地遍历它——当然,根据您的数据安排,取出测验问题和选项

之后,使用索引来记住哪个问题正在进行,当用户按下下一步/上一步按钮时,计算正确的开始索引,并从我们刚才阅读的数据中提取字符串


并在您的UI组件上显示它们,我相信这里完成的所有工作:)

我认为最好将所有问题加载到列表中。下面是如何做到这一点

1.创建一个类来容纳一个问题。 这个类是必需的,这样你就可以把问题存储在内存中。您可以使用(问题列表)存储问题列表

Public Class Question

    Public Property Question As String
    Public Property Choice1 As String
    Public Property Choice2 As String
    Public Property Choice3 As String
    Public Property Choice4 As String
    Public Property Answer As String

End Class
2.将文本文件中的所有问题加载到列表中。 在表单级别声明这两个变量

Private currentQuestion As Integer
Private listOfQuestions As List(Of Question) = New List(Of Question)
然后在表单的加载事件中编写一些代码

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    ' open a text file
    Using reader = New StreamReader("Quiz.txt")

        ' read a line
        Dim line = reader.ReadLine()
        'loop as long as it's not empty
        While (Not String.IsNullOrWhiteSpace(line))

            ' create a question instance and put the data into it
            Dim question = New Question
            question.Question = line
            question.Choice1 = reader.ReadLine()
            question.Choice2 = reader.ReadLine()
            question.Choice3 = reader.ReadLine()
            question.Choice4 = reader.ReadLine()
            question.Answer = reader.ReadLine()

            ' add the question into the list
            listOfQuestions.Add(question)

            ' read next question
            line = reader.ReadLine()
        End While

    End Using

    ' load first question
    If listOfQuestions.Count > 0 Then
        LoadQuestion(0)
    End If

End Sub
3.在表单中添加方法以将问题加载到表单中。 我重命名控件的名称,以反映它们的用法

Sub LoadQuestion(questionIndex As Integer)

    Dim question = listOfQuestions(questionIndex)
    currentQuestion = questionIndex

    With question
        lblQuestion.Text = .Question
        radChoice1.Text = .Choice1
        radChoice2.Text = .Choice2
        radChoice3.Text = .Choice3
        radChoice4.Text = .Choice4
    End With

End Sub
4.为“上一个”和“下一个”按钮创建事件处理程序。
Private Sub btnPreviousQuestion\u Click(发送者作为System.Object,e作为System.EventArgs)处理btnPreviousQuestion。单击
如果(当前问题>0),则
加载问题(当前问题-1)
如果结束
端接头
私有子btnNextQuestion_Click(发件人作为System.Object,e作为System.EventArgs)处理btnNextQuestion。单击
如果(currentQuestion
我希望它足够清晰,因为我在代码中添加了一些注释

完整表格的代码:
Imports System.IO
公开课表格1
作为整数的私有问题
私人问题列表作为列表(问题)=新列表(问题)
私有子表单1_Load(发送方作为System.Object,e作为System.EventArgs)处理MyBase.Load
'打开一个文本文件
使用reader=newstreamreader(“quick.txt”)
“读一行
尺寸线=读取器.ReadLine()
'循环,只要它不是空的
While(不是String.IsNullOrWhiteSpace(行))
'创建一个问题实例并将数据放入其中
模糊问题=新问题
问题,问题=行
question.Choice1=reader.ReadLine()
question.Choice2=reader.ReadLine()
question.Choice3=reader.ReadLine()
question.Choice4=reader.ReadLine()
question.Answer=reader.ReadLine()
'将问题添加到列表中
问题列表。添加(问题)
读下一个问题
line=reader.ReadLine()
结束时
终端使用
“第一个问题
如果问题列表.计数>0,则
问题(0)
如果结束
端接头
子负载问题(问题索引为整数)
Dim问题=问题列表(问题索引)
currentQuestion=questionIndex
有疑问
lblQuestion.Text=.Question
radChoice1.Text=.Choice1
radChoice2.Text=.Choice2
radChoice3.Text=.Choice3
radChoice4.Text=.Choice4
以
端接头
私有子btnPreviousQuestion_Click(发件人作为System.Object,e作为System.EventArgs)处理btnPreviousQuestion。单击
如果(当前问题>0),则
加载问题(当前问题-1)
如果结束
端接头
私有子btnNextQuestion_Click(发件人作为System.Object,e作为System.EventArgs)处理btnNextQuestion。单击
如果(currentQuestion

如果您需要访问当前问题的答案,可以直接从列表中获取。使用问题列表(currentQuestion)。回答以获得当前答案。

我认为最好将所有问题加载到列表中。下面是如何做到这一点

1.创建一个类来容纳一个问题。 这个类是必需的,这样你就可以把问题存储在内存中。您可以使用(问题列表)存储问题列表

Public Class Question

    Public Property Question As String
    Public Property Choice1 As String
    Public Property Choice2 As String
    Public Property Choice3 As String
    Public Property Choice4 As String
    Public Property Answer As String

End Class
2.将文本文件中的所有问题加载到列表中。 在表单级别声明这两个变量

Private currentQuestion As Integer
Private listOfQuestions As List(Of Question) = New List(Of Question)
然后在表单的加载事件中编写一些代码

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    ' open a text file
    Using reader = New StreamReader("Quiz.txt")

        ' read a line
        Dim line = reader.ReadLine()
        'loop as long as it's not empty
        While (Not String.IsNullOrWhiteSpace(line))

            ' create a question instance and put the data into it
            Dim question = New Question
            question.Question = line
            question.Choice1 = reader.ReadLine()
            question.Choice2 = reader.ReadLine()
            question.Choice3 = reader.ReadLine()
            question.Choice4 = reader.ReadLine()
            question.Answer = reader.ReadLine()

            ' add the question into the list
            listOfQuestions.Add(question)

            ' read next question
            line = reader.ReadLine()
        End While

    End Using

    ' load first question
    If listOfQuestions.Count > 0 Then
        LoadQuestion(0)
    End If

End Sub
3.在表单中添加方法以将问题加载到表单中。 我重命名控件的名称,以反映它们的用法

Sub LoadQuestion(questionIndex As Integer)

    Dim question = listOfQuestions(questionIndex)
    currentQuestion = questionIndex

    With question
        lblQuestion.Text = .Question
        radChoice1.Text = .Choice1
        radChoice2.Text = .Choice2
        radChoice3.Text = .Choice3
        radChoice4.Text = .Choice4
    End With

End Sub
4.为“上一个”和“下一个”按钮创建事件处理程序。
Private Sub btnPreviousQuestion\u Click(发送者作为System.Object,e作为System.EventArgs)处理btnPreviousQuestion。单击
如果(当前问题>0),则
加载问题(当前问题-1)
如果结束
端接头
私有子btnNextQuestion_单击(发送者作为System.Object,e作为System.EventAr