从 VB 中的文本文件中的索引读取一行

Reading a line from index in Text file in VB

我正在创建一个测验应用程序,其中我在单个文本中添加了所有问题和选项 file.But 我不知道如何在单击下一个按钮和上一个问题时移动到下一个问题我单击上一个 button.The 我目前面临的问题是从文本文件中读取特定行。

这是下一个按钮的功能

 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

下一个按钮可以使用perfectly.But我不知道如何阅读前几行。

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

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)

那你就可以自由的遍历了,把考题和选项拿出来,当然要根据你的数据排列。

之后,使用索引来记住正在处理的问题,当用户按下 next/previous 按钮时,计算正确的起始索引,并从 datas[=20] 中获取字符串=] 我们刚刚读过。

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

我认为最好将所有问题加载到一个列表中。方法如下。

1。创建一个class来保存一个问题。

需要这个 class 以便您可以将问题存储在内存中。您可以使用 List(Of Question) 来存储问题列表。

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。将文本文件中的所有问题加载到列表中。

在表单级别声明这 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。为 Previous 和 Next 按钮创建事件处理程序。

Private Sub btnPreviousQuestion_Click(sender As System.Object, e As System.EventArgs) Handles btnPreviousQuestion.Click

    If (currentQuestion > 0) Then
        LoadQuestion(currentQuestion - 1)
    End If

End Sub

Private Sub btnNextQuestion_Click(sender As System.Object, e As System.EventArgs) Handles btnNextQuestion.Click

    If (currentQuestion < listOfQuestions.Count - 1) Then
        LoadQuestion(currentQuestion + 1)
    End If

End Sub

由于我在代码中添加了一些注释,因此我希望它足够清楚。

完整表格代码:

Imports System.IO

Public Class Form1

    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

    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

    Private Sub btnPreviousQuestion_Click(sender As System.Object, e As System.EventArgs) Handles btnPreviousQuestion.Click

        If (currentQuestion > 0) Then
            LoadQuestion(currentQuestion - 1)
        End If

    End Sub

    Private Sub btnNextQuestion_Click(sender As System.Object, e As System.EventArgs) Handles btnNextQuestion.Click

        If (currentQuestion < listOfQuestions.Count - 1) Then
            LoadQuestion(currentQuestion + 1)
        End If

    End Sub

End Class

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