How to Create a Question and Answer (QA) Revision Program in Visual Basic
Submitted by Yorkiebar on Sunday, September 29, 2013 - 10:06.
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...
Step 2:
Now for the code. First we want to import System.IO for the reading of our QA text file...
Then we want to define some variables...
They are all quite self-explanatory.
Step 3:
Next we want to make our Button1 load our QA file.
Step 4:
up next we have the simple code for starting our QA test.
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...
Project Complete!
That's it! Below is the full source code and download to the project files.
- Username:Yorkiebar
- Language:Visual Basic
- Watching:Family Guy
- website:sourcecodester
- Imports System.IO
- Dim questions As New List(Of String)
- Dim answers As New List(Of String)
- Dim hasQA As Boolean = False
- Dim curQuestion As Integer = -1
- Dim ranQuestions As Integer = 0
- Dim rand As Random = New Random()
- Dim rightQuestions As New List(Of String)
- Dim wrongQuestions As New List(Of String)
- Dim fo As New OpenFileDialog
- fo.RestoreDirectory = True
- fo.ShowDialog()
- If (ranQuestions > 0) Then ranQuestions = 0
- If (My.Computer.FileSystem.FileExists(fo.FileName)) Then
- Using sr As New streamreader(fo.FileName)
- While sr.peek <> -1
- Dim line As String = sr.ReadLine()
- If (line.Contains(":")) Then
- If (Not hasQA) Then hasQA = True
- Dim splits As String() = line.Split(":")
- questions.Add(splits(0))
- answers.Add(splits(1).ToLower())
- End If
- End While
- End Using
- End If
- curQuestion = rand.Next(questions.Count())
- label4.Text = questions(curQuestion)
- ranQuestions += 1
- Dim ans As String = TextBox1.Text.ToLower()
- If (ans = answers(curQuestion)) Then
- rightQuestions.add(curQuestion)
- Else : wrongQuestions.add(curQuestion)
- End If
- TextBox1.Text = ""
- If (Not ranQuestions >= NumericUpDown1.Value) Then
- Dim tempR As Integer = curQuestion
- Do Until Not tempR = curQuestion
- tempR = rand.Next(questions.Count())
- Loop
- curQuestion = tempR
- Label4.Text = questions(curQuestion)
- ranQuestions += 1
- Else
- MsgBox("Total Right: " & rightQuestions.Count() & vbNewLine & "Total Wrong: " & wrongQuestions.Count())
- Dim wrongQs As String = ""
- For Each wrong As String In wrongQuestions
- If (wrongQs.Count() > 0) Then
- wrongQs &= ", " & questions(wrong)
- Else
- wrongQs &= questions(wrong)
- End If
- Next
- If (wrongQs.Count() > 0) Then MsgBox("Wrong Questions: " & vbNewLine & wrongQs)
- End If
- Imports System.IO
- Public Class Form1
- Dim questions As New List(Of String)
- Dim answers As New List(Of String)
- Dim hasQA As Boolean = False
- Dim curQuestion As Integer = -1
- Dim ranQuestions As Integer = 0
- Dim rand As Random = New Random()
- Dim rightQuestions As New List(Of String)
- Dim wrongQuestions As New List(Of String)
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim fo As New OpenFileDialog
- fo.RestoreDirectory = True
- fo.ShowDialog()
- If (ranQuestions > 0) Then ranQuestions = 0
- If (My.Computer.FileSystem.FileExists(fo.FileName)) Then
- Using sr As New streamreader(fo.FileName)
- While sr.peek <> -1
- Dim line As String = sr.ReadLine()
- If (line.Contains(":")) Then
- If (Not hasQA) Then hasQA = True
- Dim splits As String() = line.Split(":")
- questions.Add(splits(0))
- answers.Add(splits(1).ToLower())
- End If
- End While
- End Using
- End If
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- curQuestion = rand.Next(questions.Count())
- label4.Text = questions(curQuestion)
- ranQuestions += 1
- End Sub
- Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
- Dim ans As String = TextBox1.Text.ToLower()
- If (ans = answers(curQuestion)) Then
- rightQuestions.add(curQuestion)
- Else : wrongQuestions.add(curQuestion)
- End If
- TextBox1.Text = ""
- If (Not ranQuestions >= NumericUpDown1.Value) Then
- Dim tempR As Integer = curQuestion
- Do Until Not tempR = curQuestion
- tempR = rand.Next(questions.Count())
- Loop
- curQuestion = tempR
- Label4.Text = questions(curQuestion)
- ranQuestions += 1
- Else
- MsgBox("Total Right: " & rightQuestions.Count() & vbNewLine & "Total Wrong: " & wrongQuestions.Count())
- Dim wrongQs As String = ""
- For Each wrong As String In wrongQuestions
- If (wrongQs.Count() > 0) Then
- wrongQs &= ", " & questions(wrong)
- Else
- wrongQs &= questions(wrong)
- End If
- Next
- If (wrongQs.Count() > 0) Then MsgBox("Wrong Questions: " & vbNewLine & wrongQs)
- End If
- End Sub
- 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
Add new comment
- Add new comment
- 508 views