下一个按钮,用于抓取文本文件中的下一行文本

时间:2015-02-11 01:31:33

标签: vb.net forms file

我还在尝试学习如何在vb.net中进行测验。我越走越近了。我有一个按钮,下一个读取,一个读取提交。我有一个标签,上面写着:问题1:以及包含问题的文本框。我在第一个文本框下面有一个文本框,它有多个选择答案。如何让我的下一个按钮读取我的文本文件中的下一个问题,以便在点击下一个按钮时,该人可以选择一个答案,然后转到下一个问题?

到目前为止,这是我的代码:

Private Sub Button40_Click(sender As Object, e As EventArgs) Handles Button40.Click
    Dim FILE_NAME As String = "C:\Quiz\GenesisQuiz.txt"
    If System.IO.File.Exists(FILE_NAME) = True Then
        Dim objReader As New System.IO.StreamReader(FILE_NAME)
        TextBox1.Text = objReader.ReadToEnd & vbCrLf
        objReader.Close()
    Else
        MessageBox.Show("Wrong answer, please try again!")
    End If
End Sub

这当然会将第一个问题读入标签旁边的文本框问题1:如果我在文本文件中放入更多行文本,则所有行都会被读入文本框。 谢谢您的帮助。我认为它与For语句有关:

For i As Integer = 1 To lineNumber - 1

学习很有趣!

这是在正确的轨道上吗?

    Public Class Quiz
    Public question As String
    Public choices As String
    Public answer As String
    Public Score As Integer
    Public Sub New(question As String, choices As String, answer As String, score As Integer)
        Me.question = question
        Me.choices = Choices
        Me.answer = Answer
        Me.Score = Score
    End Sub
End Class

Private Sub Button40_Click(sender As Object, e As EventArgs) Handles Button40.Click
    Dim QuizList As New List(Of Quiz)()
End Sub
End Class

继续学习!谢谢你的回复!

1 个答案:

答案 0 :(得分:0)

感谢Plutonix和BobRodes帮助我!

Public Class Form1
Dim lines() As String
Dim answers() As String
Dim index As Integer = 0

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    lines = IO.File.ReadAllLines("C:\Quiz\GenesisQuiz.txt")
    answers = IO.File.ReadAllLines("C:\Quiz\GenesisAnswers.txt")

Private Sub Button40_Click(sender As Object, e As EventArgs) Handles Button40.Click
    TextBox1.Text &= lines(index) & Environment.NewLine
    index += 1
    If index > lines.GetUpperBound(0) Then Button40.Enabled = False
End Sub

Private Sub Button41_Click(sender As Object, e As EventArgs) Handles Button41.Click
    TextBox1.Clear()
End Sub

Private Sub Button40_MouseClick(sender As Object, e As MouseEventArgs) Handles Button40.MouseClick
    If File.Exists("C:\Quiz\GenesisAnswers.txt") Then
        Dim ioFile As New StreamReader("C:\Quiz\GenesisAnswers.txt")
        TextBox2.Text = ioFile.ReadLine()
        TextBox3.Text = ioFile.ReadLine()
        TextBox4.Text = ioFile.ReadLine()
        TextBox5.Text = ioFile.ReadLine()
    End If
End Sub

Private Sub Button41_MouseClick(sender As Object, e As MouseEventArgs) Handles Button41.MouseClick
    TextBox2.Clear()
    TextBox3.Clear()
    TextBox4.Clear()
    TextBox5.Clear()
End Sub

所以这就是我目前为止的测验。还有一段距离,但到达那里。还在学习和玩乐。随意提出建设性的批评!!!