Visual Basic 2005 Guide
Structures & Text Files
Introduction
This page contains instructions to make a simple quiz in Visual Basic. It introduces the Structure statement for creating user-defined data types and how to read from a text file containing the question data for the quiz.
The Structure Statement
The data types we have used so far only store one type of information. The structure statement allows you to create variables which store more than one type of information.
For each question in our quiz there are several data items to be stored,
- Question Text
- 4 Possible Answers
- The Correct Answer
This translates to Visual Basic as the following code,
Private Structure myQuestion
Public strQuestionText As String
Public strSuggestion() As String
Public strAnswer As String
End Structure
Study this code carefully and then copy it to the code window (at the top where global variable declarations go.
Just below the structure statement, we will declare the variables that we need for the program. The first line declares an array of variables using our structure.
Dim strTheQuestions(9) As myQuestion
Dim intCurrentQuestion As Integer
Dim intCorrect As Integer
Adding The Text File To The Project
To add the text file to the project, click on Add Existing Item on the Project menu. Choose the questions file. In the properties for the new object change the value of the Copy To Output Folder to Always.
Reading The Text File
We need to read values from the text file to store in our data structure.
The first job is to make sure that our program references the correct code libraries for File IO operations. Above the Public Class Quiz line at the top of your program, write the following lines,
Imports system
Imports System.IO
The following code reads from the text file and stores the results in our structure. This code should be placed in the load event handler for the form.
'open up a stream to the file we need
Using sr As StreamReader = File.OpenText("questions.txt")
'counter variables for loops to read in question data
Dim intQuestionCounter As Integer, intAnswerCounter As Integer
'Loops through each question
For intQuestionCounter = 0 To 9
'reads the question text of thenext item
strTheQuestions(intQuestionCounter).strQuestionText = sr.ReadLine()
'initialises the array of answers
ReDim strTheQuestions(intQuestionCounter).strSuggestion(3)
'reads in the set of suggestions for this question
For intAnswerCounter = 0 To 3
strTheQuestions(intQuestionCounter).strSuggestion(intAnswerCounter) = sr.ReadLine()
Next
'reads the answer for the question
strTheQuestions(intQuestionCounter).strAnswer = sr.ReadLine()
Next
End Using
Initialising Global Variables
The variables we declared at the top of the form need to have their initial values set, we do this beneath the code we have already written inside the load event handler for the form.
intCurrentQuestion = 0
intCorrect = 0
Designing The Form
The form is set up to be able to display a question at a time. There is a label called lblQuestionText, a list box called lstAnswers and a button called btnNextQuestion. Your form should look something like the image below,
Writing A Procedure To Display A Question
Questions will be displayed on the form on 10 occasions. It makes sense to create reusable code to do this. The following procedure can be used to display the correct information in our controls.
Private Sub showQuestion()
lblQuestionText.Text = (intCurrentQuestion + 1) & ". " & strTheQuestions(intCurrentQuestion).strQuestionText
Dim intcounter As Integer
lstAnswers.Items.Clear()
For intcounter = 0 To 3
lstAnswers.Items.Add(strTheQuestions(intCurrentQuestion).strSuggestion(intcounter))
Next
End Sub
The last line of code within the form's load event handler should be a call to this procedure,
showQuestion()
Programming The Button
When the button is pressed, we need to check that the user has chosen an answer. If they have, we compare their answer with the actual answer. If they are correct, we add 1 to their score and increase the question counter by 1. If all of the questions have been answered, we tell the user their score.
If lstAnswers.SelectedIndex = -1 Then Exit Sub
Dim strUserAnswer As String
strUserAnswer = lstAnswers.SelectedItem
If strUserAnswer = strTheQuestions(intCurrentQuestion).strAnswer Then
intCorrect = intCorrect + 1
End If
intCurrentQuestion = intCurrentQuestion + 1
If intCurrentQuestion = 10 Then
MsgBox("The quiz is over, you scored " & intCorrect & " out of 10.")
Else
showQuestion()
End If
Extensions To The Program
Better Feedback / Alternative Input Methods
The program functions in a very basic way. There is little feedback to the user and there are many ways to allow the user to select an answer. You could change the theme of the quiz - and make the questions a tad more challenging. You could also build on the code that you have copied from this page to make a Who Wants To Be A Millionaire? game. You could also tell the user which questions they got wrong and end the quiz in a less abrupt manner.
More File Handling
You will need to use the MSDN help file that comes with Visual Basic. Search the help to find out how to write information to a text file. There are several examples showing how to do this. You could then make an editor to allow you to design quizzes to use with the program.
You might also want to use the OpenFileDialog and the SaveFileDialog to allow you to choose the files that you want to edit.
A final enhancement would allow the user to choose their quiz by selecting the appropriate quiz file.