从VB中的文本文件中读取行

时间:2015-12-26 10:45:00

标签: vb.net logic readline

我正在VB中创建一个测验应用程序,测验应用程序从已创建的文本文件中读取问题,并且其中有一些问题。

1
Q.Who won the WorldCup last time?
I Don't know
May be he knows
Who knows
Who cares?
2
Question 2
Answer A
Answer B
Answer C
Answer D
3
Question 3
Ans 1
Ans 2
Ans 3
Ans 4

第一行是问题编号,第二行是问题,第3 - 6行代表答案选择。现在我已经为测验创建了一个表单,当按下下一个按钮时,它应显示下一个问题,即:在第一个问题之后,它应该转到问题2并相应地打印。但遗憾的是,我无法计算下一个问题的逻辑。

Public Class Quiz
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
    End Function
    Dim SCORE As Integer = 0
    Dim val As Integer = 30
    Dim QUES As Integer = 0
    Dim Line As Integer = 1
    Dim allLines As List(Of String) = New List(Of String)
    Dim TextFilePath As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Quiz.txt")

    Private Sub Quiz_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = 30
        Timer1.Enabled = True
        Next_Prev_Ques(Line + QUES)


    End Sub
    Public Function Next_Prev_Ques(quesno As Integer) As Integer
        Line = quesno
        Using file As New System.IO.StreamReader(TextFilePath)

            Do While Not file.EndOfStream
                allLines.Add(file.ReadLine())
            Loop

        End Using
        QUES = ReadLine(Line, allLines)
        Label1.Text = ReadLine(Line + 1, allLines)
        RadioButton1.Text = ReadLine(Line + 2, allLines)
        RadioButton2.Text = ReadLine(Line + 3, allLines)
        RadioButton3.Text = ReadLine(Line + 4, allLines)
        RadioButton4.Text = ReadLine(Line + 5, allLines)
        Return Line
    End Function
    Public Function ReadLine(lineNumber As Integer, lines As List(Of String)) As String
        Return lines(lineNumber - 1)
    End Function

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        ProgressBar1.Value += 1
        val -= 1
        Label2.Text = val & " Sec"
        If ProgressBar1.Value = ProgressBar1.Maximum Then
            Timer1.Enabled = False
        End If
        If ProgressBar1.Value > 25 Then
            SendMessage(ProgressBar1.Handle, 1040, 2, 0)
        End If
    End Sub

    Private Sub Quiz_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        Form1.Close()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox(Line + QUES + 5)
        Next_Prev_Ques(Line + QUES + 4)
        Me.Refresh()

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Next_Prev_Ques(Line + QUES + 5)
    End Sub

函数Next_Prev_Ques应该相应地运行但不是。任何人都可以发布正确的代码吗?

1 个答案:

答案 0 :(得分:1)

下面是一些使用序列化来获得相同结果的代码。您可以在其上创建一个名为问题和属性的类,如问题编号,问题和答案,将数据存储到xml文件中并使用字符串方法检索它们。检查以下代码:

班级的代码

Public Class clsQuestions
Private _Number As String
Private _Question As String
Private _Answer As String

Public Property Number() As String
    Get
        Number = _Number
    End Get
    Set(ByVal Value As String)
        _Number = Value
    End Set
End Property

Public Property Question() As String
    Get
        Question = _Question
    End Get
    Set(ByVal Value As String)
        _Question = Value
    End Set
End Property

Public Property Answer() As String
    Get
        Answer = _Answer
    End Get
    Set(ByVal Value As String)
        _Answer = Value
    End Set
End Property
End Class

表格的代码

Imports System.IO
Imports System.Xml.Serialization
Public Class Form1
Dim numQuestions() As String
Dim questions() As String
Dim answersGroup() As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles      MyBase.Load
    Label1.Text = ""
    Label2.Text = ""
    Label3.Text = ""
    Main()
End Sub

Private Sub Main()

    Dim p As New clsQuestions
    p.Number = "1,2,3,4,5,6,7,8,9,10"
    p.Question = "Question 1,Question 2,Question 3," &
        "Question 4,Question 5,Question 6,Question 7," &
        "Question 8,Question 9,Question 10"
    p.Answer = "Answer 1.1,Answer 1.2,Answer 1.3,Answer 1.4;" &
        "Answer 2.1,Answer 2.2,Answer 2.3,Answer 2.4;" &
        "Answer 3.1,Answer 3.2,Answer 3.3,Answer 3.4;" &
        "Answer 4.1,Answer 4.2,Answer 4.3,Answer 4.4;" &
        "Answer 5.1,Answer 5.2,Answer 5.3,Answer 5.4;" &
        "Answer 6.1,Answer 6.2,Answer 6.3,Answer 6.4;" &
        "Answer 7.1,Answer 7.2,Answer 7.3,Answer 7.4;" &
        "Answer 8.1,Answer 8.2,Answer 8.3,Answer 8.4;" &
        "Answer 9.1,Answer 9.2,Answer 9.3,Answer 9.4;" &
        "Answer 10.1,Answer 10.2,Answer 10.3,Answer 10.4"

    'Serialize object to a text file.
    Dim objStreamWriter As New    StreamWriter("C:\Users\Username\Documents\Questions.xml")
    Dim x As New XmlSerializer(p.GetType)
    x.Serialize(objStreamWriter, p)
    objStreamWriter.Close()


    'Deserialize text file to a new object.
    Dim objStreamReader As New StreamReader("C:\Users\Username\Documents\Questions.xml")
    Dim p2 As New clsQuestions
    p2 = x.Deserialize(objStreamReader)
    objStreamReader.Close()

    numQuestions = p2.Number.Split(",")
    questions = p2.Question.Split(",")
    answersGroup = p2.Answer.Split(";")
End Sub

Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
    Static x As Integer
    If x <= questions.Length - 1 Then
        Label1.Text = ""
        Label2.Text = ""
        Label3.Text = ""
        arrayIndex(x)
        x += 1
    End If
End Sub
Private Sub arrayIndex(ByVal num As Integer)
    Label1.Text = numQuestions(num)
    Label2.Text = questions(num)
    Dim answers() As String
    answers = answersGroup(num).Split(",")
    For Each item As String In answers
        Label3.Text &= item & Environment.NewLine
    Next
End Sub
End Class