Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Alright another schism in my code


LordChiron's Avatar
Member
0 0

[php]Private Sub rbs_CheckedChanged(sender As Object, e As EventArgs) Handles rb4.CheckedChanged, rb3.CheckedChanged, rb2.CheckedChanged, rb1.CheckedChanged 'when we call LoadQuestion we don't want any of these events to trigger 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[/php]

Ok, rb.Text needs to match the answer. my text file gets parsed with this:

[php]Private Sub loadme() Handles Me.Load Using sr As New StreamReader("C:\Quiz\questions.txt") While Not sr.EndOfStream Dim data() = sr.ReadLine.Split(","c) // parsing Dim qAndA As New QuestionWithAnswers qAndA.ItemNumber = Integer.Parse(data(0)) qAndA.Question = data(1) Dim answers() = data(2).Split("|"c) // parsing For Each answer In answers qAndA.Answers.Add(New QuestionWithAnswers.Answer With {.AnswerText = answer, .AnswerChecked = False}) Next qAndA.CorrectAnswer = data(3) // holds value of answer AllQuestions.Add(qAndA) End While End Using // get the first question qIndex starts at 0 - first element curQandA = AllQuestions.ElementAt(qIndex) LoadQuestion() End Sub[/php]

this is my text file format:

[php] 1,question, answer|answer|answer|answer, correct answer[/php]

This is how my radiobuttons are setup:

[php] Private Sub LoadQuestion() If AllQuestions.Count = 0 Then Exit Sub // little protection updatingQuestion = True curQandA = AllQuestions.ElementAt(qIndex) lbQuestion.Text = curQandA.Question // displays question rb1.Text = curQandA.Answers.ElementAt(0).AnswerText // displays answer rb2.Text = curQandA.Answers.ElementAt(1).AnswerText
rb3.Text = curQandA.Answers.ElementAt(2).AnswerText rb4.Text = curQandA.Answers.ElementAt(3).AnswerText rb1.Checked = curQandA.Answers.ElementAt(0).AnswerChecked // pick an answer rb2.Checked = curQandA.Answers.ElementAt(1).AnswerChecked rb3.Checked = curQandA.Answers.ElementAt(2).AnswerChecked rb4.Checked = curQandA.Answers.ElementAt(3).AnswerChecked updatingQuestion = False End Sub[/php]

This is my message box saying how many right out of number of questions:

[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 // check answer against correct answer correctAnswers += 1 // increment correct answer Exit For End If Next Next MessageBox.Show(String.Format("You have correctly answered {0} out of {1} questions!", correctAnswers, AllQuestions.Count.ToString)) End Sub[/php]

Now when I go to submit my answers whether I submit 1 answer or all answers at the end….the only answers I get right are the answers that are in the first slot of my text file. If the correct answer is not at the front slot then it would read it as a correct answer.

example: rb1 is checked which is Monday 1,what is today?, Monday|Tuesday|Wednesday|Thursday, Monday <—––This will show a correct answer and give a point

rb2 is checked which is Monday 2,what is today?, Tuesday|Monday|Wednessday|Thursday, Monday <——This will not register as a correct answer even though I checked the right radio button.

A little confused, could someone help me find the issue? Thanks!


LordChiron's Avatar
Member
0 0

Found the schism…it was with my questions.txt format.

I changed it to: [php]1,question,answer|answer|answer|answer,correct answer[/php] as opposed to this: [php]1,question, answer|answer|answer|answer, correct answer[/php] problem solved - quiz works like a charm - took out the white spaces in my change. Thanks for all who had a look!