By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,658 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,658 IT Pros & Developers. It's quick & easy.

VB.NET - Windows App: Loop / MsgBox help needed

P: 2
I apologize in advance for being REALLY detailed/verbose. It's the result of staring/tweaking code for too long.

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
  1. Private Sub cmdSubmit_Click(...) Handles cmdSubmit.Click
  2.         Dim sr As IO.StreamReader = IO.File.OpenText("QUESTIONS.TXT")
  3.         Dim question, choicea, choiceb, choicec, choiced, realanswer As String
  4.         Dim useranswer As String = ""
  5.         Dim score As Double
  6.         Do While sr.Peek <> -1
  7.             txtQuestion.Clear()
  8.             lstChoices.Items.Clear()
  9.             question = sr.ReadLine
  10.             choicea = sr.ReadLine
  11.             choiceb = sr.ReadLine
  12.             choicec = sr.ReadLine
  13.             choiced = sr.ReadLine
  14.             txtQuestion.Text = question
  15.             With lstChoices.Items
  16.                 .Add(choicea)
  17.                 .Add(choiceb)
  18.                 .Add(choicec)
  19.                 .Add(choiced)
  20.             End With
  21.             realanswer = sr.ReadLine
  22.             useranswer = txtAnswer.Text.ToLower() ///If there is no input, wait until there is some input!!!
  23.             If useranswer = realanswer Then
  24.                 MsgBox("Good job!", 0, "Correct")
  25.                 score += 1
  26.                 txtAnswer.Clear()
  27.             ElseIf (useranswer = "a") Or (useranswer = "b") Or (useranswer = "c") Or (useranswer = "d") Then
  28.                 MsgBox("Sorry, the answer was " & realanswer & ".", 0, "Incorrect")
  29.                 txtAnswer.Clear()
  30.             Else
  31.                 MsgBox("That is not a valid choice.  Please try again", 0, "Invalid Choice")
  32.                 txtAnswer.Clear()
  33.                 txtAnswer.Focus()
  34.             End If
  35.         Loop
  36.         sr.Close()
  37.         txtAnswer.Enabled = False
  38.         cmdSubmit.Enabled = False
  39.         lblInstructions.Focus()
  40.         txtScore.Text = "Your score is " & score & "."
  41.     End Sub
Thanks so much in advance!!
Nov 21 '07 #1
Share this Question
Share on Google+
2 Replies


Shashi Sadasivan
Expert 100+
P: 1,435
Hi,
i think this is a windows application!

why is all your code in the submit button?

according to this code...
when the program starts, there should be nothing regarding the queastions appearing.

when the user clicks the submit button the question file will be read, and then the code will run, andit will not wait for any user input until it has scrolled through alll the questions in the file!

So the conclusion is that when you read one question set from the file, you stop until the submit button is pressed

when the submit bbuton is pressed, check if the answer is right
if right, then advance reading the next question, else do nothing.
Nov 21 '07 #2

P: 2
Okay, gotcha -- I'll try it out. It's all in one procedure now, but once I get it working properly, then I'll divide it up into sub procedures. Thanks for your help, Shashi!


Hi,
i think this is a windows application!

why is all your code in the submit button?

according to this code...
when the program starts, there should be nothing regarding the queastions appearing.

when the user clicks the submit button the question file will be read, and then the code will run, andit will not wait for any user input until it has scrolled through alll the questions in the file!

So the conclusion is that when you read one question set from the file, you stop until the submit button is pressed

when the submit bbuton is pressed, check if the answer is right
if right, then advance reading the next question, else do nothing.
Nov 21 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.