This is from the Starting out with Visual Basic 2010 5th edition book by Gaddis,
ID: 3534670 • Letter: T
Question
This is from the Starting out with Visual Basic 2010 5th edition book by Gaddis, it is from chapter 8, question #4 in programming challenges. The answer is not complete though in the answers for the book, they only created the 1st form for it.
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 multi-
ple 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
Your application should store the correct answers in an array. A form, such as the
one shown in Figure 8-48, should allow the user to enter answers for each question.
When the user clicks the Score Exam button, the application should display another
form showing whether each question was answered correctly or incorrectly, and
whether the student passed or failed the exam. A student must correctly answer 15
of the 20 questions to pass the exam.
Input validation: Only accept the letters A, B, C, or D as answers.
Explanation / Answer
Here is the 100% working code
Private Sub btnScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScore.Click
'Correct answers as an array
Dim achrCorrectAnswers() As String = {"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"}
Dim achrUserAnswers(19) As String 'Textboxes as an array
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
Dim intCorrect As Integer = 0
For x As Integer = 0 To 19
If achrCorrectAnswers(x) = achrUserAnswers(x) Then
achrUserAnswers(x) = "Correct"
intCorrect += 1
Else
achrUserAnswers(x) = "Incorrect"
End If
Next
Dim AnswersForm As New frmAnswers
AnswersForm.lstAns.Items.AddRange(achrUserAnswers)
'Display the total incorrect answers in the listbox
AnswersForm.lstAns.Items.Add("You have a total of " & (20 - intCorrect).ToString() & " Incorrect answers.")
'Display the total correct answers in the listbox
AnswersForm.lstAns.Items.Add("and a total of " & intCorrect.ToString() & " Correct answers.")
'If the user has 15 or more correct answers, then the user pass the exam
If intCorrect > 14 Then
AnswersForm.lstAns.Items.Add("Congratulations! You have passed the Driver's License Exam.")
'Else, the user fail the exam
Else
AnswersForm.lstAns.Items.Add("You have failed the Driver's License Exam. Try Again.")
End If
'Display the results in another form
AnswersForm.ShowDialog()
End Sub
to restrict user input, set your textboxes maxlength property to 1 + charactercasing property to Upper, then add this code:
Private Sub txt1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt9.KeyPress, txt8.KeyPress, txt7.KeyPress, txt6.KeyPress, txt5.KeyPress, txt4.KeyPress, txt3.KeyPress, txt20.KeyPress, txt2.KeyPress, txt19.KeyPress, txt18.KeyPress, txt17.KeyPress, txt16.KeyPress, txt15.KeyPress, txt14.KeyPress, txt13.KeyPress, txt12.KeyPress, txt11.KeyPress, txt10.KeyPress, txt1.KeyPress
If Not "ABCD".Contains(Char.ToUpper(e.KeyChar)) AndAlso Not Char.IsControl(e.KeyChar) Then
e.Handled = True
End If
End Sub
Dont forget to rate..
Cheers!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.