PSN PSN - 1 year ago 81 Question

Reading lines from a text file in VB

I'm creating a Quiz Application in VB and the quiz app reads the questions from a text file which is already created and it has got some questions in it.

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

The first line is the question number,the second line is the question,lines 3 - 6 represents the answer choices.Now I have created a Form for quiz and when the next button is pressed it should display the next question i.e,After the first question it should go to Question 2 and print accordingly.But unfortunately i'm unable to calculate the logic to go to next question.

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

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
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)

End Sub

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

The function
Should run accordingly but its not.Can anyone post the correct code?

Answer Source

Below is some code that uses serialization to get the same results. You can create a class called Questions and properties on it like questionnumber, question and answer, store the data into a xml file and retrieve them with string methods. Check the code below:

The code for the class

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

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

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

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

The code for the form

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 = ""
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)

    '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)

    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 = ""
        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
End Sub
End Class
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download