A1VBCode Forums

Submit button function


http://www.a1vbcode.com/vbforums/Topic32257.aspx

By Luigi Nesbitt - 2/14/2015

This is a vb.net app for a quiz I've been working on. It is not, I stress, not a homework assignment as I am 20+ years out of school. My question, how would I code a submit button to check the answer when someone clicks a RadioButton. I have a quiz that gives 4 options for answers, 1 is correct. This is my format in a txt file: 1,question1, answer1|answer2|answer3|answer4, correct answer



This is my variable



Public Property CorrectAnswer As String




This is my form1_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)

LoadQuestion()

UpdateLabel()

End While

End Using




This is my Radiobutton (Handles all 4)





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




This is my sub LoadQuestion()



Private Sub LoadQuestion()

If AllQuestions.Count = 0 Then Exit Sub

updatingQuestion = True

curQandA = AllQuestions.ElementAt(qIndex)

TextBox1.Text = curQandA.Question

TextBox2.Text = curQandA.Answers.ElementAt(0).AnswerText

TextBox3.Text = curQandA.Answers.ElementAt(1).AnswerText

TextBox4.Text = curQandA.Answers.ElementAt(2).AnswerText

TextBox5.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




This is my next button (my previous button is similiar)



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

End If

If _myIndex < 4 Then

_myIndex += 1

UpdateLabel()

End If






Now I just need to code my submit button to check answer in variable CorrectAnswer! Any help in the right direction appreciated!



New to vb.net!