Using VB 2005:
I need to design a multiple choice test (unfortunately, I can't use radio buttons).
How it works...
Pre-1) File consists of the following format:
Question 1
Choice 1
Choice 2
Choice 3
Choice 4
Correct Choice
Question 2, etc.
1) I get my input from the file described above file (and display it)
2) The user must type the answer (letter) in a text box
3) The user clicks a button (to submit the answer), and the appropriate action is taken:
Correct -> MsgBox with correct message -> go to next question
Incorrect -> MsgBox with incorrect message -> go to next question
Invalid answer -> MsgBox with invalid message -> try again (until a valid message is received)
The first set of questions come in when the form loads -- that's working fine.
User = me
I type in an answer, and the MsgBox comes up -- that's working fine.
Problem: I click OK in the MsgBox...
a) For *all* answers, the question advances -- it should only do that for correct and incorrect answers (but not for invalid answers).
b) The program doesn't wait for me to put in the answer to the next question, so it just comes up as a blank, which is an invalid answer. (Go to problem a.) I click OK, and it repeats until the file ends.
How can I solve these issues? I know that they need to have another loop or two in there somewhere, but it seems like I've tried every loop placement that there is and none work.
Here is my code for this private sub activated by clicking the button, and you'll see where I need for it to "wait" for input:
Expand|Select|Wrap|Line Numbers
- Private Sub cmdSubmit_Click(...) Handles cmdSubmit.Click
- Dim sr As IO.StreamReader = IO.File.OpenText("QUESTIONS.TXT")
- Dim question, choicea, choiceb, choicec, choiced, realanswer As String
- Dim useranswer As String = ""
- Dim score As Double
- Do While sr.Peek <> -1
- txtQuestion.Clear()
- lstChoices.Items.Clear()
- question = sr.ReadLine
- choicea = sr.ReadLine
- choiceb = sr.ReadLine
- choicec = sr.ReadLine
- choiced = sr.ReadLine
- txtQuestion.Text = question
- With lstChoices.Items
- .Add(choicea)
- .Add(choiceb)
- .Add(choicec)
- .Add(choiced)
- End With
- realanswer = sr.ReadLine
- useranswer = txtAnswer.Text.ToLower() ///If there is no input, wait until there is some input!!!
- If useranswer = realanswer Then
- MsgBox("Good job!", 0, "Correct")
- score += 1
- txtAnswer.Clear()
- ElseIf (useranswer = "a") Or (useranswer = "b") Or (useranswer = "c") Or (useranswer = "d") Then
- MsgBox("Sorry, the answer was " & realanswer & ".", 0, "Incorrect")
- txtAnswer.Clear()
- Else
- MsgBox("That is not a valid choice. Please try again", 0, "Invalid Choice")
- txtAnswer.Clear()
- txtAnswer.Focus()
- End If
- Loop
- sr.Close()
- txtAnswer.Enabled = False
- cmdSubmit.Enabled = False
- lblInstructions.Focus()
- txtScore.Text = "Your score is " & score & "."
- End Sub