vb.net coding help
Need some help coding the submit button in my multiple choice quiz application. Here is my quiz code in full - Edit Update
[php] Public Class QuestionWithAnswers
Public Property ItemNumber As Integer
Public Property Question As String
Public Property Answers As New List(Of Answer)
Public Property CorrectAnswer As String
Public Class Answer
Public AnswerText As String
Public AnswerChecked As Boolean
End Class
End Class
Private AllQuestions As New List(Of QuestionWithAnswers)
Private qIndex As Integer = 0
Private updatingQuestion As Boolean
Private curQandA As QuestionWithAnswers
Private _myIndex As Integer = 1
Sub UpdateLabel()
lblQuesNum.Text = (qIndex + 1).ToString
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
curQandA = AllQuestions.ElementAt(qIndex)
If qIndex < 3 Then ' is it safe to increment
qIndex += 1 'it is, 'then set the current question here since we are using an index
LoadQuestion()
UpdateLabel()
End If
End Sub
Private Sub LoadQuestion()
If AllQuestions.Count = 0 Then Exit Sub
updatingQuestion = True
curQandA = AllQuestions.ElementAt(qIndex)
tb1.Text = curQandA.Question
tb2.Text = curQandA.Answers.ElementAt(0).AnswerText
tb3.Text = curQandA.Answers.ElementAt(1).AnswerText
tb4.Text = curQandA.Answers.ElementAt(2).AnswerText
tb5.Text = curQandA.Answers.ElementAt(3).AnswerText
RadioButton1.Checked = curQandA.Answers.ElementAt(0).AnswerChecked
RadioButton2.Checked = curQandA.Answers.ElementAt(1).AnswerChecked
RadioButton3.Checked = curQandA.Answers.ElementAt(2).AnswerChecked
RadioButton4.Checked = curQandA.Answers.ElementAt(3).AnswerChecked
updatingQuestion = False
End Sub
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged, RadioButton3.CheckedChanged, RadioButton2.CheckedChanged, RadioButton1.CheckedChanged
If Not updatingQuestion Then
Dim rb = DirectCast(sender, RadioButton)
Dim qandaAnswer = curQandA.Answers.Where(Function(a) a.AnswerText = rb.Text).FirstOrDefault
If Not qandaAnswer Is Nothing Then qandaAnswer.AnswerChecked = True
For Each ans In curQandA.Answers
If ans Is qandaAnswer Then
ans.AnswerChecked = True
Else
ans.AnswerChecked = False
End If
Next
End If
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
curQandA = AllQuestions.ElementAt(qIndex)
If qIndex > 0 Then
qIndex -= 1
LoadQuestion()
UpdateLabel()
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using sr As New StreamReader("C:\Quiz\questions.txt")
While Not sr.EndOfStream
Dim data() = sr.ReadLine.Split(","c)
Dim qAndA As New QuestionWithAnswers
qAndA.ItemNumber = Integer.Parse(data(0))
qAndA.Question = data(1)
Dim answers() = data(2).Split("|"c)
For Each answer In answers
qAndA.Answers.Add(New QuestionWithAnswers.Answer With {.AnswerText = answer, .AnswerChecked = False})
Next
qAndA.CorrectAnswer = data(3)
AllQuestions.Add(qAndA)
UpdateLabel()
LoadQuestion()
End While
End Using
End Sub
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Dim correctAnswers As Integer = 0
For Each question As QuestionWithAnswers In AllQuestions
For Each answer As QuestionWithAnswers.Answer In question.Answers
If answer.AnswerChecked AndAlso answer.AnswerText = question.CorrectAnswer Then
correctAnswers += 1
Exit For
End If
Next
Next
End Sub[/php]
Obviously this is not php but the markup
tags were not working.
Last Code Block - btnSubmit_Click
Each Answer has 2 properties: AnswerText and AnswerChecked. When someone makes a choice this Answer will have the AnswerChecked set to true. So in the loop you will be looking for the Answer.AnswerChecked = true then you compare the Answer.AnswerText with the question.CorrectAnswer text.
[php]Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click Dim correctAnswers As Integer = 0 For Each question As QuestionWithAnswers In AllQuestions For Each answer As QuestionWithAnswers.Answer In question.Answers If answer.AnswerChecked AndAlso answer.AnswerText = question.CorrectAnswer Then correctAnswers += 1 Exit For End If Next Next[/php]
I had to compare the answer text with the correct answer to increment the point system. Thanks for having a look.