473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
2 4835
Shashi Sadasivan
1,435 Expert 1GB
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
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

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

Similar topics

9
by: none | last post by:
Hello all, I wrote a shell program a few years ago in VB6 that needs to be modified. The problem I have is this: The SysAdmin uses this shell in place of Explorer, so there is no taskbar. When...
2
by: Dave | last post by:
I have developed a VB.net application that transfers a SOAP wrapped message to another server securely(using SSL and sever/client certificates). When I run this app from a Windows 2000...
0
by: Andrew Dowding | last post by:
Hi Everybody, I have been looking at problems with my Windows Forms C# application and it's little Jet 4 (Access) database for the last few days. The Windows Forms app implements a facade and...
4
by: billsahiker | last post by:
Why is my application crashing on a windows 98 machine? It runs fine on the development machine with xp pro. I installed framework 2, with dotnetfx.exe, and windows installer 2 on the windows 98...
28
by: | last post by:
I have a multi threaded windows form application that runs great after calling Application.Run(). Application.Run is required for a COM component I a using in the app (required for message loop). ...
2
by: LostDeveloper via AccessMonster.com | last post by:
Relationship goes Certification can have multiple variations (any single variation satifies the certification), which require multiple course groups which require any one of the products assigned...
9
by: Mohitz | last post by:
Hi, How do you write a Windows GUI application in C++ ?? And is C++ the best language to write GUI applications in? Thank you Mohit
12
by: Anil Gupte | last post by:
I wrote my Windows Service first as a regular Windows Exe because it is easier to debug. In that I used AppDir = Application.ExecutablePath.Substring(0,...
2
by: Semajthewise | last post by:
Ok guys, This is a quick overview of what I'm trying to do. 2 textboxes. textbox1 has a fraction entered by the user into it in one of these 2 ways 1(space)3/4 or simply 3/4 Now what this code is...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.