Option Strict On
Option Explicit On
Public Class Form1
Dim rnd As New System.Random()
Private Const WordFilePath As String = "C:\My Documents\Visual Studio 2005\Projects\Quiz\Quiz\Words"
Private Const WordFileFormat As String = WordFilePath & "\{0}.txt"
'Contains the List of words
Dim CurrentWordList As ArrayList
'Contains the current Question
Dim CurrentQuestion As Question
Dim Correct As Integer ' A count of how many correct questions
Dim Incorrect As Integer ' a count of the incorrect questions
'Simple Structure to contain the question
Private Structure Question
Dim Question As String
Dim Answer As String
Public Shared Function ToQuestion(ByVal Question As String, ByVal Answer As String) As Question
Dim Q As New Question
Q.Question = Question
Q.Answer = Answer
Return Q
End Function
End Structure
'Loads the Word file into an ArrayList
Private Function ParseWordList(ByVal WordFile As String) As ArrayList
Dim Words As New ArrayList
Dim sr As New System.IO.StreamReader(WordFile)
Do While Not sr.EndOfStream
Dim value() As String = sr.ReadLine().Split(" "c)
Words.Add(Question.ToQuestion(value(0), value(1)))
Loop
Return Words
End Function
Private Function RandomQuestion(ByRef WordList As ArrayList) As Question
' this should be moved to global scope
'This doesn't need any comments *GRIN*
'Get a random Index
Dim RandomIndex As Integer = rnd.Next(0, WordList.Count)
'Get the Random Question
Dim result As Question = CType(WordList.Item(RandomIndex), Question)
WordList.RemoveAt(RandomIndex)
Return result
End Function
Private Function ProcessAnswer(ByRef Question As Question, ByVal Answer As String) As Boolean
If Question.Answer.ToLower = Answer.ToLower Then
'Correct
Correct += 1
Question = Nothing
Return True
Else
Question = Nothing
Incorrect += 1
Return False
End If
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim WordLists() As String = System.IO.Directory.GetFiles(WordFilePath, "*.txt")
For Each f As String In WordLists
Dim fi As New System.IO.FileInfo(f)
lstWordLists.Items.Add(fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length))
Next
End Sub
Private Sub lstWordLists_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstWordLists.SelectedIndexChanged
' This allows the user to change the word list.
If lstWordLists.SelectedIndex() = -1 Then
'Nothing Selected, bail
Exit Sub
End If
Dim strWordFile As String = String.Format(WordFileFormat, lstWordLists.SelectedItem)
If Not System.IO.File.Exists(strWordFile) Then
'There is something wrong,
Exit Sub ' Bail
End If
'Set the Current Word List
CurrentWordList = ParseWordList(strWordFile)
End Sub
'The user clicks this button to start or move to the next question
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
' If there is no current question, load it.
If IsNothing(CurrentQuestion.Question) Then
If Not CurrentWordList.Count = 0 Then
'New Question...
CurrentQuestion = RandomQuestion(CurrentWordList)
TextBox1.Text = CurrentQuestion.Question
Else
MessageBox.Show("Text Complete!")
End If
Else
'I'm assuming there should be no case sensitivity (remove the .ToLower's if not)
If ProcessAnswer(CurrentQuestion, TextBox2.Text) Then
'User answered question Correctly
MessageBox.Show("Correct")
Me.Text = String.Format("Correct: {0}, Incorrect: {1}!", Correct, Incorrect)
'
TextBox2.Text = ""
TextBox1.Text = ""
Else
Me.Text = String.Format("Correct: {0}, Incorrect: {1}!", Correct, Incorrect)
MessageBox.Show("Incorrect")
'User Answered Question Wrong
End If
End If
End Sub
End Class