Click here to Skip to main content
15,909,896 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

VB
Public Class frmJPDLExamm
    Dim intMinutes As Integer   'Holds the minutes for the timer
    Private Sub btnScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScore.Click
        Dim AnswersForm As New frmAnswers
        'Correct answers as an array
        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     'Counter for incorrect answers
        Dim intCorrect As Integer       'Counter for correct answers
        Dim intCount As Integer         'Total counter
        Dim chrInput As Char            'Hold a char value
        Dim achrUserAnswers(19) As Char '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

        Try
            'Convert the user input from a textbox to a char
            strInput = CChar(achrUserAnswers(19))
            For intCount = 0 To 19
                'If the user's answer from the textbox is equals to the correct answer
                'from the corresponding array, then the answer is correct
                If achrUserAnswers(19) = achrCorrectAnswers(19) Then
                    intCorrect += 1
                    AnswersForm.lstAns.Items.Add("Correct")
                    'Else, the user's answer is incorrect
                Else
                    intIncorrect += 1
                    AnswersForm.lstAns.Items.Add("Incorrect")
                    intCount += 1
                End If
            Next
        Catch
            'Error message for invalid input
            MessageBox.Show("Enter letter A, B, C, or D only.")
        End Try

        'Display the total incorrect answers in the listbox
        AnswersForm.lstAns.Items.Add("You have a total of " & intIncorrect.ToString() & " Incorrect answers.")
        intCorrect = 20 - intIncorrect
        '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

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        'Clears all textboxes. From textbox 1 to 20.
        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
        'Close the form
        Me.Close()
    End Sub
    Private Sub btnToggle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToggle.Click
        'Toggle the timer
        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
        'Update the minutes display by one minute every minute
        intMinutes += 1
        lblCounter.Text = intMinutes.ToString()
    End Sub
End Class
Posted
Comments
Sergey Alexandrovich Kryukov 15-Nov-11 17:50pm    
Let's read the question: "Motor Vehicles office has asked you to create an application". Who "you"?! This is your homework which you did not even bother to re-formulate as a question from yourself. If you are asking to do some homework for you, this is cheating. Read other answers -- did you see us doing it?

And your code is impossibly bad.

If you allow yourself to repeat lines like

achrUserAnswers(0) = txt1.Text
achrUserAnswers(1) = txt2.Text
achrUserAnswers(2) = txt3.Text
achrUserAnswers(3) = txt4.Text...

It means you are doing something opposite to programming in principle... It leads you nowhere.

--SA
flaafee 15-Nov-11 17:57pm    
I stated that the question with the "you" is for reference.
The repeated lines are declaring a textbox as an array. Since there are 20 textboxes the user puts the answer in.
Hopefully, this clarifies your concerns.
Sander Rossel 16-Nov-11 2:12am    
As much as I agree with you I believe the OP is indeed very stuck here and deserves some kind of answer. I didn't answer his question directly, just gave him some directions.

1 solution

In most cases where the answer is multiple choice you would use either a CheckBox[^] or a RadioButton[^] Control...
What you are trying to accomplish is pretty unusual and not very intuitive, but if you really want to do it, check this: Text box to accept only number[^]
Those TextBoxes accept only numbers, but of course you could also check for A, B, C or D instead of for number. The code is C#, but a convertor[^] should help you translate it to VB. It's just a little bit of code.
Hope it helps.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 15-Nov-11 17:51pm    
This is not the only reason to say that OP's code goes nowhere...
Please read the comment to this question.
--SA
flaafee 15-Nov-11 18:02pm    
i would make it a radio button or a drop down menu but the challenge was to 'Input validation: Only accept the letters A, B, C, or D as answers'. So I'm kind of stuck with the textboxes. Thank you for the help though. :)
Sander Rossel 16-Nov-11 2:07am    
You can 'close' this thread yourself by accepting any answer that helped you ;)
It will let people know you've been helped.
No problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900