[Help] with a Quiz program in Visual Basic

Posts 1–13 of 13 · Page 1 of 1
[Help] with a Quiz program in Visual Basic
im making this multiple choices quiz problem for my class, i had most part coded and the question+answers of the quiz are on the .txt file placed in the debug folder. the purpose is to click next button then the question+answers will change to the next set of the question+answers and i got this part working, but i got troubled on the part where i need to show "correct" or "incorrect" at the end of the test, and it will go into the labels i create, seems only worked for the first question+answers.
also, how can i make it end at the end of the test? it seems the program will keep on going when i click Next button until theres no line to read, then error appear. need some sort of notification like a messagebox? but i dont know where i should put it.
last, how can i fit 3 lines of paragraphs(questions) on one RichTextBox, it doesn't work when i put all 2 or 3 lines of paragraphs together.
and here's the code
[highlight=vb.net]Public Class frm_Lab6c

Dim Line1(10) As String
Dim Line2(10) As String
Dim Line3(10) As String
Dim Line4(10) As String
Dim Line5(10) As String
Dim line6(10) As String
Dim Line7(10) As String
Dim Line8(10) As String
Dim Answer(10) As String

Dim passagequestion As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sr As IO.StreamReader = IO.File.OpenText("Summarization Practice Questions 2-11.txt")
Dim i As Integer = 0
Do Until sr.EndOfStream

Line1(i) = sr.ReadLine
Line2(i) = sr.ReadLine
Line3(i) = sr.ReadLine
Line4(i) = sr.ReadLine
Line5(i) = sr.ReadLine
line6(i) = sr.ReadLine
Line7(i) = sr.ReadLine
Line8(i) = sr.ReadLine
Answer(i) = sr.ReadLine
i += 1

Loop

txtOne.Text = Line1(0)

radOne.Text = Line4(0)
radTwo.Text = Line5(0)
radThree.Text = line6(0)
radFour.Text = Line7(0)
radFive.Text = Line8(0)
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

Dim msg As String
Dim title As String
Dim style As MsgBoxStyle
Dim response As MsgBoxResult
msg = "Are you sure you want to quit this test?"
style = MsgBoxStyle.DefaultButton2 Or _
MsgBoxStyle.Critical Or MsgBoxStyle.YesNo
title = "WARNING"
response = MsgBox(msg, style, title)
If response = MsgBoxResult.Yes Then
ExitApplication()
Else

End If


End Sub
Public Sub ExitApplication()

If MessageBox.Show("You Have Failed!", "End of the Test", _
MessageBoxButtons.YesNo, MessageBoxIcon.Error) _
= DialogResult.Yes Then
Application.Exit()
Else
NotFunctional()
End If
End Sub
Public Sub NotFunctional()
MessageBox.Show("Too Late, You Already Failed!", "WARNING")
Application.Exit()
End Sub


Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click

passagequestion += 1
txtOne.Text = Line1(passagequestion)

radOne.Text = Line4(passagequestion)
radTwo.Text = Line5(passagequestion)
radThree.Text = line6(passagequestion)
radFour.Text = Line7(passagequestion)
radFive.Text = Line8(passagequestion)

End Sub
Private Sub RadioButtonOne()

If radOne.Checked = True And Answer(passagequestion) = "1" Then
lblOne.Text = ("Correct")
ElseIf radTwo.Checked = True And Answer(passagequestion) = "2" Then
lblOne.Text = ("Correct")
ElseIf radThree.Checked = True And Answer(passagequestion) = "3" Then
lblOne.Text = ("Correct")
ElseIf radFour.Checked = True And Answer(passagequestion) = "4" Then
lblOne.Text = ("Correct")
ElseIf radFive.Checked = True And Answer(passagequestion) = "5" Then
lblOne.Text = ("Correct")
Else
lblOne.Text = ("Incorrect")
End If
End Sub
Private Sub RadioButtonTwo()

If radTwo.Checked = True And Answer(passagequestion) = "1" Then
lblTwo.Text = ("Correct")
ElseIf radTwo.Checked = True And Answer(passagequestion) = "2" Then
lblTwo.Text = ("Correct")
ElseIf radThree.Checked = True And Answer(passagequestion) = "3" Then
lblTwo.Text = ("Correct")
ElseIf radFour.Checked = True And Answer(passagequestion) = "4" Then
lblTwo.Text = ("Correct")
ElseIf radFive.Checked = True And Answer(passagequestion) = "5" Then
lblTwo.Text = ("Correct")
Else
lblTwo.Text = ("Incorrect")
End If
End Sub


Private Sub radOne_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radOne.CheckedChanged
RadioButtonOne()

End Sub

Private Sub radTwo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radTwo.CheckedChanged
RadioButtonOne()

End Sub

Private Sub radThree_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radThree.CheckedChanged
RadioButtonOne()

End Sub

Private Sub radFour_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radFour.CheckedChanged
RadioButtonOne()

End Sub

Private Sub RadioFive_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radFive.CheckedChanged
RadioButtonOne()

End Sub



End Class
[/highlight]

Any help will be appreciated.
Sincerely.
Could you post this in a more easy-to-read format? I have no idea what you're trying to do, and seems like you used workarounds instead of the harder(but more correct) ways.

Why don't you code an admin form that you set My.Settings.Question1 .. etc from?
Hi, thanks for the reply
i put some descriptions in the code
i hope this helps
i need a way to add second line under the first line in textbox, because some problems have 2 or 3 paragraphs. and line1 is a paragraph, line2 and line3 is second and third paragraph.then the rest is the answer choices. i tried to put line1 and line2 together, it wont show up properly.
then i need a notification when i click Next on the last quiz problem, i dont know there i could put this message box.
last, the very frustrating thing is to show the word "correct" or "incorrect" in the label box at the end of the test for each set of problem, without messin gup other labels




[highlight=vb.net]Public Class frm_Lab6c
'these declare the lines, the problem and the answer choices
Dim Line1(10) As String
Dim Line2(10) As String
Dim Line3(10) As String
Dim Line4(10) As String
Dim Line5(10) As String
Dim line6(10) As String
Dim Line7(10) As String
Dim Line8(10) As String
Dim Answer(10) As String

Dim passagequestion As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sr As IO.StreamReader = IO.File.OpenText("Summarization Practice Questions 2-11.txt")
Dim i As Integer = 0
Do Until sr.EndOfStream
'reads the text file containing the problem+choices when form starts
Line1(i) = sr.ReadLine
Line2(i) = sr.ReadLine
Line3(i) = sr.ReadLine
Line4(i) = sr.ReadLine
Line5(i) = sr.ReadLine
line6(i) = sr.ReadLine
Line7(i) = sr.ReadLine
Line8(i) = sr.ReadLine
Answer(i) = sr.ReadLine
i += 1

Loop

txtOne.Text = Line1(0)

radOne.Text = Line4(0)
radTwo.Text = Line5(0)
radThree.Text = line6(0)
radFour.Text = Line7(0)
radFive.Text = Line8(0)
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

Dim msg As String 'ignore the button, it's quit button.
Dim title As String
Dim style As MsgBoxStyle
Dim response As MsgBoxResult
msg = "Are you sure you want to quit this test?"
style = MsgBoxStyle.DefaultButton2 Or _
MsgBoxStyle.Critical Or MsgBoxStyle.YesNo
title = "WARNING"
response = MsgBox(msg, style, title)
If response = MsgBoxResult.Yes Then
ExitApplication()
Else

End If


End Sub
Public Sub ExitApplication() 'for quit button

If MessageBox.Show("You Have Failed!", "End of the Test", _
MessageBoxButtons.YesNo, MessageBoxIcon.Error) _
= DialogResult.Yes Then
Application.Exit()
Else
NotFunctional()
End If
End Sub
Public Sub NotFunctional()
MessageBox.Show("Too Late, You Already Failed!", "WARNING")
Application.Exit()
End Sub


Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
'next button, clicked to get to next set of the problem+answer choices

passagequestion += 1
txtOne.Text = Line1(passagequestion)

radOne.Text = Line4(passagequestion)
radTwo.Text = Line5(passagequestion)
radThree.Text = line6(passagequestion)
radFour.Text = Line7(passagequestion)
radFive.Text = Line8(passagequestion)

End Sub
Private Sub RadioButtonOne() 'this is to show first correct answer

If radOne.Checked = True And Answer(passagequestion) = "1" Then
lblOne.Text = ("Correct")
ElseIf radTwo.Checked = True And Answer(passagequestion) = "2" Then
lblOne.Text = ("Correct")
ElseIf radThree.Checked = True And Answer(passagequestion) = "3" Then
lblOne.Text = ("Correct")
ElseIf radFour.Checked = True And Answer(passagequestion) = "4" Then
lblOne.Text = ("Correct")
ElseIf radFive.Checked = True And Answer(passagequestion) = "5" Then
lblOne.Text = ("Correct")
Else
lblOne.Text = ("Incorrect")
End If
End Sub
Private Sub RadioButtonTwo() 'show correct answer two, but i dont knw how to put it together.

If radTwo.Checked = True And Answer(passagequestion) = "1" Then
lblTwo.Text = ("Correct")
ElseIf radTwo.Checked = True And Answer(passagequestion) = "2" Then
lblTwo.Text = ("Correct")
ElseIf radThree.Checked = True And Answer(passagequestion) = "3" Then
lblTwo.Text = ("Correct")
ElseIf radFour.Checked = True And Answer(passagequestion) = "4" Then
lblTwo.Text = ("Correct")
ElseIf radFive.Checked = True And Answer(passagequestion) = "5" Then
lblTwo.Text = ("Correct")
Else
lblTwo.Text = ("Incorrect")
End If
End Sub


Private Sub radOne_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radOne.CheckedChanged
RadioButtonOne() 'this puts the "correct" or "incorrect" in the label, i tried to put RadioButtonTwo() under this b4, but it showed on two labels. i want 1 label for each problems.

End Sub

Private Sub radTwo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radTwo.CheckedChanged
RadioButtonOne()

End Sub

Private Sub radThree_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radThree.CheckedChanged
RadioButtonOne()

End Sub

Private Sub radFour_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radFour.CheckedChanged
RadioButtonOne()

End Sub

Private Sub RadioFive_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radFive.CheckedChanged
RadioButtonOne()

End Sub



End Class
[/highlight]
Second line under first line:

Textbox1.MultiLine = True

Notification:

Just call Msgbox("failed") or Msgbox("passed")

Correct/Incorrect: just create a new label, visible = FALSE

then when they enter their answer
Label1.visible = True
Label1.Text = "pass" or "fail"

Then when they click the next question button...
Label1.visible = false


Does that help?
thank you, i'll try it.

edit: where do i put the msgbox under? thats wher im confused with.
Find the last button
Double click it
Put it there
Hmm, I'm bored at the moment, so gimme a snippet of what the input file looks like (I'm guessing 10 questions each with 8 possible choices?) And I'll look into running up something more efficient.
Why dont you do something like (Example):
Dim ArrayQuestions(10) As String
Dim ArrayAnswers(10) As String

Then in somewhere you do:
ArrayQuestions(1) = "How old are you?"
ArrayAnswers(1) = "Im 5 Years old.."
etc.. You make all question and all answers for them..

Something like that.. And to show them, You do:

LabelQuestion.Text = ArrayQuestion(1)
LabelAnswers.Text = ArrayAnswers(1)

In a button or something:
ButaoNext_Click
If LabelQuestion.Text = ArrayQuestion(1) Then
LabelQuestion.Text = ArrayQuestion(2)
LabelAnswers.Text = ArrayAnswers(2)
End if

Just a example Its something like that xDD.
I think you missed something there Away. He's already using arrays . How he is using them can be improved though.

EDIT: 3500 posts...coolman
Ooh, I made something very similar for my teacher at school.

I used Access to store my questions and answers in seperate tables.
I'd have questions in one of them and the possible answers to these questions in the other one, and in the questions table I'd also have a column telling you exactly which answer is the correct one.

Then just have your program read in the first questions if you press next it reads the next question, if you press previous it would go back to the previous question allowing you to change your answer if you want that ofcourse. And on startup you'd make a temporary table storing the given answer and correct answer to the given question and once you get to the end of the test, let the user decide whether they wanna end the test and know their results or continue and once they're done you go through the temp table checking which one is correct and which one isn't.

This solution gives you the ability to give an amount of questions only limited by the time you're willing to spend on it.

Ofcourse you'd have to be able to safely write away everything to make sure they don't change their results manualy afterwards.
thank you all.
Pastebin.com

here's the problem and the answers, the choices is actually a summarizing of the paragraphs, and the individual number under the answer choices represent the correct answer. i made the paragraphs and choices line1, line2 respectively and so on... i made these spaces under some paragraphs to present line2 and line3, because some problems has 3 paragraphs.

i want to make the program to have a notification at the end of the test, like MessageBox("click continue", "End of the Test") then i need to show 10 labels when i click continue. something like that.
Hmm. I think I have an idea. Gimme a minute.

EDIT: Has the basic functionality.

What you need to do is add 5 radiobuttons to your form (if you're going to rename them, you will need to edit some code but essentially just make sure they have an increasing numeric value in them somewhere).

[highlight=vb.net]
Imports System.Text.RegularExpressions
Public Class Form1

Private questionList As New List(Of QuizQuestion)
Private curQuestion As Integer = 0

'when you load the form, parse the questions from the textfile to the list.
Private Sub _Startup(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ParseQuestions(questionList, "C:\Users\Jason_2\Documents\Snippets Vault\Questions.txt")
End Sub

Public Sub ParseQuestions(ByRef QuestionList As List(Of QuizQuestion), ByVal txtLocation As String)
Dim iterator As Integer = 0
Dim lineArray As String() = IO.File.ReadAllLines(txtLocation).Where(Function(l ine As String) Not String.IsNullOrEmpty(line)).ToArray

For i As Integer = 0 To lineArray.Count()
Dim curline As String = ""
Dim answerList As New List(Of String)
Dim question As String = ""
Dim answer As Integer = 0
Do
curline = lineArray(iterator)
If curline.Length > 3 AndAlso Regex.IsMatch(curline.Substring(0, 3), "[a-zA-Z]\.\s") Then
answerList.Add(curline.Split(New Char() {" "c}, 2)(1))
ElseIf Not IsNumeric(curline) Then
If curline.Contains(".") AndAlso IsNumeric(curline.Split(New Char() {"."c}, 2)(0)) Then curline = curline.Split(New Char() {"."c}, 2)(1)
question &= curline & " "
Else
answer = Integer.Parse(curline) - 1 'this will be the index in the array, as arrays are zero based and the 'answer' is not, we need to subtract one to account for zero-base.
End If
iterator += 1
Loop While (Not IsNumeric(curline) AndAlso Not iterator >= lineArray.Count)
QuestionList.Add(New QuizQuestion(question, answerList.ToArray, answer))
i = iterator
Next
End Sub

Public Sub SelectQuestion(ByVal qNumber As Integer, ByVal qList As List(Of QuizQuestion))
Dim currentQ As QuizQuestion = qList(qNumber - 1)
Me.questionLabel.Text = currentQ.Question
For i As Integer = 1 To 5
Dim rb As RadioButton = CType(Me.GroupBox1.Controls("RadioButton" & i.ToString), RadioButton) 'my radiobuttons were nested in a groupbox, hence Me.GroupBox1.Controls rather than Me.Controls.
rb.Text = currentQ.Answers(i - 1)
Next
End Sub
'example of a possible "Next Question" button, increase the question number, then get the question at that number. (NOT ZERO BASED QNO)
Private Sub NextQuestion_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
curQuestion += 1
SelectQuestion(curQuestion, questionList)
End Sub

Private Function CheckAnswer(ByVal qNumber As Integer, ByVal qList As List(Of QuizQuestion)) As Boolean
Dim rb As RadioButton = (From r As RadioButton In Me.GroupBox1.Controls.OfType(Of RadioButton)() Where r.Checked = True Select r).Single
Dim chosenAnswer As Integer = Integer.Parse(rb.Name.Replace("RadioButton", "")) - 1
If chosenAnswer = qList(qNumber - 1).CorrectAnswer Then Return True
End Function

'example use of the check answer function.
Private Sub CheckAnswer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim correct As Boolean = CheckAnswer(curQuestion, questionList)
If correct Then
MsgBox("you were right")
Else
MsgBox("you were wrong")
End If
End Sub
End Class

Public Class QuizQuestion
Private qQuestion As String = String.Empty
Private qAnswers As String() = {}
Private qCorrect As Integer

Public Sub New(ByVal q As String, ByVal a As String(), ByVal c As Integer)
Me.Question = q
Me.Answers = a
Me.CorrectAnswer = c
End Sub

Public Property Question() As String
Get
Return Me.qQuestion
End Get
Set(ByVal value As String)
Me.qQuestion = value
End Set
End Property

Public Property Answers() As String()
Get
Return Me.qAnswers
End Get
Set(ByVal value As String())
Me.qAnswers = value
End Set
End Property

Public Property CorrectAnswer() As Integer
Get
Return Me.qCorrect
End Get
Set(ByVal value As Integer)
Me.qCorrect = value
End Set
End Property
End Class
[/highlight]

If you need anything explained, don't hesitate to ask.

Sometimes I amaze even myself.
ur code looks very hard and pro. i just started in couple weeks, my teacher didnt teach much. we have to read books. i dont want my teacher to ask me "did you code this?" if you can, i hope theres way to add something to my own code to make it work?
btw, the files is in a flash drive, im gonna put it in different computers. so i had coded in a way that it reads the text file directly from the "Debug" folder in side the "bin".
thank you.

edit:
i just solved 2 of my problems, but i still dont understand how to show results at all. this is wut i had originally.
then i tried to put "RadioButtonTwo()" under the radio button too, but when i go to second question problem it show result of the second problem on both labels like this:
correct answer to #1 is c, and i click c, then i have this in label1 and label2.
#1 [Correct]
#2 [Label2]

then if i go to second problem, i click on an incorrect answer lets say A, then i will get this.
#1 [Incorrect] << it changed when i get to problem 2, this follows the result of the second problem.
#2 [Incorrect]

or maybe i could use IF statement? how can i improve this?


Code:
     Private Sub RadioButtonOne()
 
        If radOne.Checked = True And Answer(passagequestion) = "1" Then
            lblOne.Text = ("Correct")
        ElseIf radTwo.Checked = True And Answer(passagequestion) = "2" Then
            lblOne.Text = ("Correct")
        ElseIf radThree.Checked = True And Answer(passagequestion) = "3" Then
            lblOne.Text = ("Correct")
        ElseIf radFour.Checked = True And Answer(passagequestion) = "4" Then
            lblOne.Text = ("Correct")
        ElseIf radFive.Checked = True And Answer(passagequestion) = "5" Then
            lblOne.Text = ("Correct")
        Else
            lblOne.Text = ("Incorrect")
        End If
    End Sub
    Private Sub RadioButtonTwo()
 
        If radTwo.Checked = True And Answer(passagequestion) = "1" Then
            lblTwo.Text = ("Correct")
        ElseIf radTwo.Checked = True And Answer(passagequestion) = "2" Then
            lblTwo.Text = ("Correct")
        ElseIf radThree.Checked = True And Answer(passagequestion) = "3" Then
            lblTwo.Text = ("Correct")
        ElseIf radFour.Checked = True And Answer(passagequestion) = "4" Then
            lblTwo.Text = ("Correct")
        ElseIf radFive.Checked = True And Answer(passagequestion) = "5" Then
            lblTwo.Text = ("Correct")
        Else
            lblTwo.Text = ("Incorrect")
        End If
    End Sub
 
 
    Private Sub radOne_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radOne.CheckedChanged
        RadioButtonOne()
       'RadioButtonTwo()
    End Sub
 
    Private Sub radTwo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radTwo.CheckedChanged
        RadioButtonOne()
 
    End Sub
 
    Private Sub radThree_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radThree.CheckedChanged
        RadioButtonOne()
 
    End Sub
 
    Private Sub radFour_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radFour.CheckedChanged
        RadioButtonOne()
 
    End Sub
 
    Private Sub RadioFive_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radFive.CheckedChanged
        RadioButtonOne()
 
    End Sub
 
 
 
End Class
Posts 1–13 of 13 · Page 1 of 1
This thread is closed for replies.

Tags for this Thread

None

Need help?