473,791 Members | 2,853 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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 4855
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
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
perkykoala
2 New Member
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
6964
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 his users run PC Anywhere from the shell, and minimize it, it minimizes to the system tray. With no task bar, there is no system tray, so there is no way to restore the PC Anywhere window. The shell starts PC Anywhere using ShellExecute. I...
2
393
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 Professional box everything works as expected, the secure channel is established and the message is sent properly. When I use the exact same app on the Windows 2000 Server(running IIS) box the app does not work properly. I have used a packet sniffer to...
0
3170
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 implementation, data abstraction layer. But because each data adapter in the implementation layer has a connection object that opens and closes as needed, I found I got several errors from the Jet engine when there were simultaneous connections to...
4
1744
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 machine. I read here that dotnetfx does not have all of the framework. could that be the problem? Some of the functions work, others do not. The ones that do not have one thing in common: they make an asynchronous call to a method in a class...
28
7379
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). I have created a windows service from VStudio 2005 template. What is the windows service replacement for Application.Run()?
2
3962
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 to the group. MS Access.. VBA.. got very many sub relationships and it takes about 30 seconds to see who is certified. But now my problem is I want to go back through the process and see who is only 1 course group away from being certified, and...
9
2797
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
3718
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, Application.ExecutablePath.LastIndexOf("\")) ' Where this application Excutable sits Now, when I am converting it to a Service, what path can I use? Or can I specify a path in the service to always use as a data path? I would prefer the former because I do not know the layout of...
2
1683
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 supposed to do is get the length of the entry ans tear it apart storing any numbers found in the string. These are later used to turn the fraction entered into a ecimal equivelent. (Note) i know I can do this with 3 boxes by having a whole...
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4109
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.