How to Create a Question and Answer (QA) Revision Program in Visual Basic

Language

Introduction: Welcome to a tutorial on how to create a revision tool in Visual Basic. It will load a list of Questions and Answers (separated by a ":") and test you on the questions. Steps of Creation: Step 1: First we want to do a couple of things: Create a form with... Label4 - Current Question Button1 - Load QA List Button2 - Start Questions Button3 - Check Answer Textbox1 - Enter Answer Create a QA list, here is mine I used for testing purposes...
  1. Username:Yorkiebar
  2. Language:Visual Basic
  3. Watching:Family Guy
  4. website:sourcecodester
Step 2: Now for the code. First we want to import System.IO for the reading of our QA text file...
  1. Imports System.IO
Then we want to define some variables...
  1. Dim questions As New List(Of String)
  2. Dim answers As New List(Of String)
  3. Dim hasQA As Boolean = False
  4. Dim curQuestion As Integer = -1
  5. Dim ranQuestions As Integer = 0
  6. Dim rand As Random = New Random()
  7. Dim rightQuestions As New List(Of String)
  8. Dim wrongQuestions As New List(Of String)
They are all quite self-explanatory. Step 3: Next we want to make our Button1 load our QA file.
  1. Dim fo As New OpenFileDialog
  2. fo.RestoreDirectory = True
  3. fo.ShowDialog()
  4. If (ranQuestions > 0) Then ranQuestions = 0
  5. If (My.Computer.FileSystem.FileExists(fo.FileName)) Then
  6. Using sr As New streamreader(fo.FileName)
  7. While sr.peek <> -1
  8. Dim line As String = sr.ReadLine()
  9. If (line.Contains(":")) Then
  10. If (Not hasQA) Then hasQA = True
  11. Dim splits As String() = line.Split(":")
  12. questions.Add(splits(0))
  13. answers.Add(splits(1).ToLower())
  14. End If
  15. End While
  16. End Using
  17. End If
Step 4: up next we have the simple code for starting our QA test.
  1. curQuestion = rand.Next(questions.Count())
  2. label4.Text = questions(curQuestion)
  3. ranQuestions += 1
Step 5: Finally we have our check button which will both check the answer, move on to the next question (unless it is at the end/numericupdown.value) and output the total right and wrong questions...
  1. Dim ans As String = TextBox1.Text.ToLower()
  2. If (ans = answers(curQuestion)) Then
  3. rightQuestions.add(curQuestion)
  4. Else : wrongQuestions.add(curQuestion)
  5. End If
  6. TextBox1.Text = ""
  7. If (Not ranQuestions >= NumericUpDown1.Value) Then
  8. Dim tempR As Integer = curQuestion
  9. Do Until Not tempR = curQuestion
  10. tempR = rand.Next(questions.Count())
  11. Loop
  12. curQuestion = tempR
  13. Label4.Text = questions(curQuestion)
  14. ranQuestions += 1
  15. Else
  16. MsgBox("Total Right: " & rightQuestions.Count() & vbNewLine & "Total Wrong: " & wrongQuestions.Count())
  17. Dim wrongQs As String = ""
  18. For Each wrong As String In wrongQuestions
  19. If (wrongQs.Count() > 0) Then
  20. wrongQs &= ", " & questions(wrong)
  21. Else
  22. wrongQs &= questions(wrong)
  23. End If
  24. Next
  25. If (wrongQs.Count() > 0) Then MsgBox("Wrong Questions: " & vbNewLine & wrongQs)
  26. End If
Project Complete! That's it! Below is the full source code and download to the project files.
  1. Imports System.IO
  2. Public Class Form1
  3. Dim questions As New List(Of String)
  4. Dim answers As New List(Of String)
  5. Dim hasQA As Boolean = False
  6. Dim curQuestion As Integer = -1
  7. Dim ranQuestions As Integer = 0
  8. Dim rand As Random = New Random()
  9. Dim rightQuestions As New List(Of String)
  10. Dim wrongQuestions As New List(Of String)
  11.  
  12. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  13. Dim fo As New OpenFileDialog
  14. fo.RestoreDirectory = True
  15. fo.ShowDialog()
  16. If (ranQuestions > 0) Then ranQuestions = 0
  17. If (My.Computer.FileSystem.FileExists(fo.FileName)) Then
  18. Using sr As New streamreader(fo.FileName)
  19. While sr.peek <> -1
  20. Dim line As String = sr.ReadLine()
  21. If (line.Contains(":")) Then
  22. If (Not hasQA) Then hasQA = True
  23. Dim splits As String() = line.Split(":")
  24. questions.Add(splits(0))
  25. answers.Add(splits(1).ToLower())
  26. End If
  27. End While
  28. End Using
  29. End If
  30. End Sub
  31.  
  32. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  33. curQuestion = rand.Next(questions.Count())
  34. label4.Text = questions(curQuestion)
  35. ranQuestions += 1
  36. End Sub
  37.  
  38. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  39. Dim ans As String = TextBox1.Text.ToLower()
  40. If (ans = answers(curQuestion)) Then
  41. rightQuestions.add(curQuestion)
  42. Else : wrongQuestions.add(curQuestion)
  43. End If
  44. TextBox1.Text = ""
  45. If (Not ranQuestions >= NumericUpDown1.Value) Then
  46. Dim tempR As Integer = curQuestion
  47. Do Until Not tempR = curQuestion
  48. tempR = rand.Next(questions.Count())
  49. Loop
  50. curQuestion = tempR
  51. Label4.Text = questions(curQuestion)
  52. ranQuestions += 1
  53. Else
  54. MsgBox("Total Right: " & rightQuestions.Count() & vbNewLine & "Total Wrong: " & wrongQuestions.Count())
  55. Dim wrongQs As String = ""
  56. For Each wrong As String In wrongQuestions
  57. If (wrongQs.Count() > 0) Then
  58. wrongQs &= ", " & questions(wrong)
  59. Else
  60. wrongQs &= questions(wrong)
  61. End If
  62. Next
  63. If (wrongQs.Count() > 0) Then MsgBox("Wrong Questions: " & vbNewLine & wrongQs)
  64. End If
  65. End Sub
  66. End Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Submitted bymatip omar peines (not verified)on Thu, 05/05/2016 - 16:30

i love grapes, and screwdrivers

Add new comment