Hi,
I'm having trouble with the Try...Catch code. I'm trying to make the textboxes only accept letters A, B, C, or D as answers from the user. I'm also having trouble matching the two arrays. I wanted it to be like
if textbox1 is A then it is correct but in a loop. Please take a look at my code. Any help is appreciated. Thank you.
This is the programming challenge instruction for reference:
The local Registry of Motor Vehicles office has asked you to create an application that grades the written portion of the driver's license exam. The exam has 20 multiple choice questions. Here are the correct answers to the questions:
1. B 6. A 11. B 16. C
2. D 7. B 12. C 17. C
3. A 8. A 13. D 18. B
4. A 9. C 14. A 19. D
5. C 10.D 15. D 20. A
Public Class frmJPDLExamm
Dim intMinutes As Integer
Private Sub btnScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScore.Click
Dim AnswersForm As New frmAnswers
Dim achrCorrectAnswers() As Char = {"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"}
Dim intIncorrect As Integer
Dim intCorrect As Integer
Dim intCount As Integer
Dim chrInput As Char
Dim achrUserAnswers(19) As Char
achrUserAnswers(0) = txt1.Text
achrUserAnswers(1) = txt2.Text
achrUserAnswers(2) = txt3.Text
achrUserAnswers(3) = txt4.Text
achrUserAnswers(4) = txt5.Text
achrUserAnswers(5) = txt6.Text
achrUserAnswers(6) = txt7.Text
achrUserAnswers(7) = txt8.Text
achrUserAnswers(8) = txt9.Text
achrUserAnswers(9) = txt10.Text
achrUserAnswers(10) = txt11.Text
achrUserAnswers(11) = txt12.Text
achrUserAnswers(12) = txt13.Text
achrUserAnswers(13) = txt14.Text
achrUserAnswers(14) = txt15.Text
achrUserAnswers(15) = txt16.Text
achrUserAnswers(16) = txt17.Text
achrUserAnswers(17) = txt18.Text
achrUserAnswers(18) = txt19.Text
achrUserAnswers(19) = txt20.Text
Try
strInput = CChar(achrUserAnswers(19))
For intCount = 0 To 19
If achrUserAnswers(19) = achrCorrectAnswers(19) Then
intCorrect += 1
AnswersForm.lstAns.Items.Add("Correct")
Else
intIncorrect += 1
AnswersForm.lstAns.Items.Add("Incorrect")
intCount += 1
End If
Next
Catch
MessageBox.Show("Enter letter A, B, C, or D only.")
End Try
AnswersForm.lstAns.Items.Add("You have a total of " & intIncorrect.ToString() & " Incorrect answers.")
intCorrect = 20 - intIncorrect
AnswersForm.lstAns.Items.Add("and a total of " & intCorrect.ToString() & " Correct answers.")
If intCorrect > 14 Then
AnswersForm.lstAns.Items.Add("Congratulations! You have passed the Driver's License Exam.")
Else
AnswersForm.lstAns.Items.Add("You have failed the Driver's License Exam. Try Again.")
End If
AnswersForm.ShowDialog()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
txt1.Clear()
txt2.Clear()
txt3.Clear()
txt4.Clear()
txt5.Clear()
txt6.Clear()
txt7.Clear()
txt8.Clear()
txt9.Clear()
txt10.Clear()
txt11.Clear()
txt12.Clear()
txt13.Clear()
txt14.Clear()
txt15.Clear()
txt16.Clear()
txt17.Clear()
txt18.Clear()
txt19.Clear()
txt20.Clear()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnToggle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToggle.Click
If tmrMinutes.Enabled = True Then
tmrMinutes.Enabled = False
btnToggle.Text = "Start &Timer"
Else
tmrMinutes.Enabled = True
btnToggle.Text = "Stop &Timer"
End If
End Sub
Private Sub tmrMinutes_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMinutes.Tick
intMinutes += 1
lblCounter.Text = intMinutes.ToString()
End Sub
End Class