I am making a quiz for my Computer Science class where there are keywords and definitions and the user has to match the correct keyword to the definition twice and until then it keeps on going while tracking the time taken. Both the keywords and the definitions have to be loaded from text files. So far I have created a module and read in two seperate text files. One for the keywords and the other for the defintions. It looks like so:

Module array

    Public Const A As String = "A) "
    Public Const B As String = "B) "
    Public Const C As String = "C) "

    Public Keywords As String() = IO.File.ReadAllLines("C:\Users\Matt\Documents\keywords.txt")
    Public Keyword1 As String = Keywords(0)
    Public Keyword2 As String = Keywords(1)
    Public Keyword3 As String = Keywords(2)
    Public Keyword4 As String = Keywords(3)
    Public Keyword5 As String = Keywords(4)
    Public Keyword6 As String = Keywords(5)
    Public Keyword7 As String = Keywords(6)
    Public Keyword8 As String = Keywords(7)
    Public Keyword9 As String = Keywords(8)
    Public Keyword10 As String = Keywords(9)
    Public Keyword11 As String = Keywords(10)
    Public Keyword12 As String = Keywords(11)
    Public Keyword13 As String = Keywords(12)
    Public Keyword14 As String = Keywords(13)
    Public Keyword15 As String = Keywords(14)

    Public Definitions As String() = IO.File.ReadAllLines("C:\Users\Matt\Documents\definitions.txt")
    Public Definition1 As String = Definitions(0)
    Public Definition2 As String = Definitions(1)
    Public Definition3 As String = Definitions(2)
    Public Definition4 As String = Definitions(3)
    Public Definition5 As String = Definitions(4)
    Public Definition6 As String = Definitions(5)
    Public Definition7 As String = Definitions(6)
    Public Definition8 As String = Definitions(7)
    Public Definition9 As String = Definitions(8)
    Public Definition10 As String = Definitions(9)
    Public Definition11 As String = Definitions(10)
    Public Definition12 As String = Definitions(11)
    Public Definition13 As String = Definitions(12)
    Public Definition14 As String = Definitions(13)
    Public Definition15 As String = Definitions(14)

End Module

On my main quiz form (not the module) I have it started (not finished as I am at home practicing) and I have managed to get the keywords to randomize but I am trying to figure out how to randomize the definition order in the radiobuttons.

My main form (started so far) looks like so:

Public Class Form1
    Dim Rand As New Random()
    Dim KeyIndex As Integer = Rand.Next(0, Keywords.Length - 1)
    Dim KeyArray As String = Keywords(KeyIndex)

    Dim DefIndex As Integer = Rand.Next(0, Definitions.Length - 1)
    Dim DefArray = Definitions(DefIndex)

    Dim KeyIndex2 As Integer = Rand.Next(0, Keywords.Length - 1)
    Dim KeyArray2 As String = Keywords(KeyIndex2)

    Dim DefIndex2 As Integer = Rand.Next(0, Definitions.Length - 1)
    Dim DefArray2 As String = Definitions(DefIndex2)
    Dim rounds As Integer = 0

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        LabelKeyword.Text = Keyword1
        If LabelKeyword.Text = Keyword1 And rounds <> 2 Then
            rounds += 1
            RadioButtonDef1.Text = A & Definition1
            RadioButtonDef2.Text = B & DefArray
            RadioButtonDef3.Text = C & DefArray2
        ElseIf LabelKeyword.Text = Keyword2 And rounds <> 2 Then
            rounds += 1
            RadioButtonDef1.Text = A & DefArray
            RadioButtonDef2.Text = B & DefArray2
            RadioButtonDef3.Text = C & Definition2
        End If
        If rounds = 2 Then
            MsgBox("Quiz Complete")
        End If
    End Sub

    Private Sub ButtonNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNext.Click
        If LabelKeyword.Text = Keyword1 And RadioButtonDef1.Checked And rounds <> 2 Then
            rounds += 1

        End If
    End Sub
End Class

Is there anyway that I can randomize the order of the definitions in the radiobuttons?

2 Years
Discussion Span
Last Post by Reverend Jim

For what you want a class would be handy. Include properties for the keyword, definition, and radiobuttons for each. Instead of randomizing what each definition radiobutton holds, randomize the location of the definitions.


You should not be declaring a separate variable for each keyword and definition. That's what a dictionary (also known as an associative array) is for.

Private myDic As New Dictionary(Of String, String)

Then you assign words and definitions as

Do while more words and definitions
    read next word from the word file
    read next definition from the definition file
    myDic.Add(word, Definition)

This assumes that the words and their associated definitions are in the same relative records in each file. For the record, storing the words and definitions in separate files is pretty asinine. For the record, I don't think this requires a separate class.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.