我正在创建一个测验应用程序,其中我已将所有问题和选项添加到单个文本文件中。但是当我单击上一个按钮时单击上一个按钮和上一个问题时,我不知道如何转到下一个问题我目前面临的问题是从文本文件中读取一个特定的行。
这是Next Button的功能
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
Next Button工作正常。但我不知道如何阅读Previous行。
答案 0 :(得分:3)
我认为将所有问题加载到列表中会更好。这是如何做到的。
此类是必需的,因此您可以将问题存储在内存中。您可以使用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个变量。
Private currentQuestion As Integer
Private listOfQuestions As List(Of Question) = New List(Of Question)
然后在表单的load事件中编写一些代码。
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
我希望它很清楚,因为我在我的代码中添加了一些注释。
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)。答案获得当前答案。
答案 1 :(得分:2)
将所有这些内容读入字符串缓冲区并进行操作比在文件流中搜索要容易得多。使用以下命令:
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组件上显示它们,我相信所有工作都在这里完成:)