Donate to us via Paypal!
It is the path of least resistance that makes rivers and men crooked. - Bj Palmer
Monday, March 01, 2021
Navigation
Home
 Find:
 Information:
Learn
Communicate
Submit
Shop
Challenges
 Exploit:
 Programming:
 Think:
 Track:
 Patch:
 Other:
 Need Help?
Other
Members Online
Total Online: 104
Guests Online: 103
Members Online: 1

Registered Members: 133766
Newest Member: denim6321d
Latest Articles

View Thread

HellBound Hackers | Computer General | Programming

Author

vb.net coding help

LordChiron
Member



Posts: 7
Location:
Joined: 23.12.13
Rank:
HBH Guru
Posted on 17-02-15 22:15
Need some help coding the submit button in my multiple choice quiz application. Here is my quiz code in full - Edit Update

PHP
  1. <?php
  2.     Public Class QuestionWithAnswers
  3.         Public Property ItemNumber As Integer
  4.         Public Property Question As String
  5.         Public Property Answers As New List(Of Answer)
  6.         Public Property CorrectAnswer As String
  7.         Public Class Answer
  8.             Public AnswerText As String
  9.             Public AnswerChecked As Boolean
  10.         End Class
  11.     End Class
  12.     Private AllQuestions As New List(Of QuestionWithAnswers)
  13.     Private qIndex As Integer = 0
  14.     Private updatingQuestion As Boolean
  15.     Private curQandA As QuestionWithAnswers
  16.     Private _myIndex As Integer = 1
  17.  
  18.     Sub UpdateLabel()
  19.         lblQuesNum.Text = (qIndex + 1).ToString
  20.     End Sub
  21.  
  22.     Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
  23.         curQandA = AllQuestions.ElementAt(qIndex)
  24.         If qIndex < 3 Then           ' is it safe to increment
  25.             qIndex += 1              'it is, 'then set the current question here since we are using an index
  26.             LoadQuestion()
  27.             UpdateLabel()
  28.         End If
  29.     End Sub
  30.  
  31.     Private Sub LoadQuestion()
  32.         If AllQuestions.Count = 0 Then Exit Sub
  33.         updatingQuestion = True
  34.         curQandA = AllQuestions.ElementAt(qIndex)
  35.         tb1.Text = curQandA.Question
  36.         tb2.Text = curQandA.Answers.ElementAt(0).AnswerText
  37.         tb3.Text = curQandA.Answers.ElementAt(1).AnswerText
  38.         tb4.Text = curQandA.Answers.ElementAt(2).AnswerText
  39.         tb5.Text = curQandA.Answers.ElementAt(3).AnswerText
  40.         RadioButton1.Checked = curQandA.Answers.ElementAt(0).AnswerChecked
  41.         RadioButton2.Checked = curQandA.Answers.ElementAt(1).AnswerChecked
  42.         RadioButton3.Checked = curQandA.Answers.ElementAt(2).AnswerChecked
  43.         RadioButton4.Checked = curQandA.Answers.ElementAt(3).AnswerChecked
  44.         updatingQuestion = False
  45.     End Sub
  46.  
  47.     Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged, RadioButton3.CheckedChanged, RadioButton2.CheckedChanged, RadioButton1.CheckedChanged
  48.         If Not updatingQuestion Then
  49.             Dim rb = DirectCast(sender, RadioButton)
  50.             Dim qandaAnswer = curQandA.Answers.Where(Function(a) a.AnswerText = rb.Text).FirstOrDefault
  51.             If Not qandaAnswer Is Nothing Then qandaAnswer.AnswerChecked = True
  52.             For Each ans In curQandA.Answers
  53.                 If ans Is qandaAnswer Then
  54.                     ans.AnswerChecked = True
  55.                 Else
  56.                     ans.AnswerChecked = False
  57.                 End If
  58.             Next
  59.         End If
  60.     End Sub
  61.     Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
  62.         curQandA = AllQuestions.ElementAt(qIndex)
  63.         If qIndex > 0 Then
  64.             qIndex -= 1
  65.             LoadQuestion()
  66.             UpdateLabel()
  67.         End If
  68.     End Sub
  69.  
  70.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  71.         Using sr As New StreamReader("C:\Quiz\questions.txt")
  72.             While Not sr.EndOfStream
  73.                 Dim data() = sr.ReadLine.Split(","c)
  74.                 Dim qAndA As New QuestionWithAnswers
  75.                 qAndA.ItemNumber = Integer.Parse(data(0))
  76.                 qAndA.Question = data(1)
  77.                 Dim answers() = data(2).Split("|"c)
  78.                 For Each answer In answers
  79.                     qAndA.Answers.Add(New QuestionWithAnswers.Answer With {.AnswerText = answer, .AnswerChecked = False})
  80.                 Next
  81.                 qAndA.CorrectAnswer = data(3)
  82.                 AllQuestions.Add(qAndA)
  83.                 UpdateLabel()
  84.                 LoadQuestion()
  85.             End While
  86.         End Using
  87.     End Sub
  88.  
  89.     Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
  90.         Dim correctAnswers As Integer = 0
  91.         For Each question As QuestionWithAnswers In AllQuestions
  92.             For Each answer As QuestionWithAnswers.Answer In question.Answers
  93.                 If answer.AnswerChecked AndAlso answer.AnswerText = question.CorrectAnswer Then
  94.                     correctAnswers += 1
  95.                     Exit For
  96.                 End If
  97.             Next
  98.         Next
  99.     End Sub
  100. ?>


Obviously this is not php but the
Code



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.

Edited by LordChiron on 18-02-15 15:51
Author

RE: Answer Found

LordChiron
Member



Posts: 7
Location:
Joined: 23.12.13
Rank:
HBH Guru
Posted on 18-02-15 15:53
PHP
  1. <?php
  2. Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
  3.         Dim correctAnswers As Integer = 0
  4.         For Each question As QuestionWithAnswers In AllQuestions
  5.             For Each answer As QuestionWithAnswers.Answer In question.Answers
  6.                 If answer.AnswerChecked AndAlso answer.AnswerText = question.CorrectAnswer Then
  7.                     correctAnswers += 1
  8.                     Exit For
  9.                 End If
  10.             Next
  11.         Next
  12. ?>


I had to compare the answer text with the correct answer to increment the point system.
Thanks for having a look.
Author

RE: vb.net coding help

Huitzilopochtli
Member



Posts: 1644
Location:
Joined: 19.02.13
Rank:
God
Posted on 18-02-15 19:46
No problem. Thumbs Up