Hi All,

I started looking for some code to randomize questions and answers. I am wondering if someone can tell me why this doesn't work when using 0 to 49 in the ArrayList? The original code was a total of 4 questions. When I use this many, there are no correct answers that load with the question. In "Sub setTheAnswer" if I set 'If totalRemove = 46" instead of 2 then the correct answer loads to button 4 repeatedly.

Code:
Public Class Form1
    Dim questions As New ArrayList
    Dim answers As New ArrayList
    Dim dtQAMain As New DataTable
    Dim questionsCopy As New ArrayList
    Dim alAnsButton As New ArrayList 'arraylist to store all answer button.
    Dim totalScore As Integer


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Method/Function for loading the Q&A
        alAnsButton.Add(Button1)
        alAnsButton.Add(Button2)
        alAnsButton.Add(Button3)
        alAnsButton.Add(Button4)

        loaddtQA()
        loadQsAndAs()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Me.Close()
    End Sub

    Private Sub loaddtQA()
        dtQAMain = New DataTable
        dtQAMain.Columns.Add("Q")
        dtQAMain.Columns.Add("A")

        For i = 0 To 49
            questions.Add("")
            answers.Add("")
        Next

        questions(0) = "Alabama?"
        questions(1) = "Alaska?"
        questions(2) = "Arizona?"
        questions(3) = "Arkansas?"
        questions(4) = "California?"
        questions(5) = "Colorado?"
        questions(6) = "Connecticut?"
        questions(7) = "Delaware?"
        questions(8) = "Florida?"
        questions(9) = "Georgia?"
        questions(10) = "Hawaii?"
        questions(11) = "Idaho?"
        questions(12) = "Illinois?"
        questions(13) = "Indiana?"
        questions(14) = "Iowa?"
        questions(15) = "Kansas?"
        questions(16) = "Kentucky?"
        questions(17) = "Louisiana?"
        questions(18) = "Maine?"
        questions(19) = "Maryland?"
        questions(20) = "Massachusetts?"
        questions(21) = "Michigan?"
        questions(22) = "Minnesota?"
        questions(23) = "Mississippi?"
        questions(24) = "Missouri?"
        questions(25) = "Montana?"
        questions(26) = "Nebraska?"
        questions(27) = "Nevada?"
        questions(28) = "New Hampshire?"
        questions(29) = "New Jersey?"
        questions(30) = "New Mexico?"
        questions(31) = "New York?"
        questions(32) = "North Carolina?"
        questions(33) = "North Dakota?"
        questions(34) = "Ohio?"
        questions(35) = "Oklahoma?"
        questions(36) = "Oregon?"
        questions(37) = "Pennsylvania?"
        questions(38) = "Rhode Island?"
        questions(39) = "South Carolina?"
        questions(40) = "South Dakota?"
        questions(41) = "Tennessee?"
        questions(42) = "Texas?"
        questions(43) = "Utah?"
        questions(44) = "Vermont?"
        questions(45) = "Virginia?"
        questions(46) = "Washington?"
        questions(47) = "West Virginia?"
        questions(48) = "Wisconsin?"
        questions(49) = "Wyoming?"

        answers(0) = "Montgomery"
        answers(1) = "Juneau"
        answers(2) = "Phoenix"
        answers(3) = "Little Rock"
        answers(4) = "Sacramento"
        answers(5) = "Denver"
        answers(6) = "Hartford"
        answers(7) = "Dover"
        answers(8) = "Tallahassee"
        answers(9) = "Atlanta"
        answers(10) = "Honolulu"
        answers(11) = "Boise"
        answers(12) = "Springfield"
        answers(13) = "Indianapolis"
        answers(14) = "Des Moines"
        answers(15) = "Topeka"
        answers(16) = "Frankfort"
        answers(17) = "Baton Rouge"
        answers(18) = "Augusta"
        answers(19) = "Annapolis"
        answers(20) = "Boston"
        answers(21) = "Lansing"
        answers(22) = "Saint Paul"
        answers(23) = "Jackson"
        answers(24) = "Jefferson City"
        answers(25) = "Helena"
        answers(26) = "Lincoln"
        answers(27) = "Carson City"
        answers(28) = "Concord"
        answers(29) = "Trenton"
        answers(30) = "Santa Fe"
        answers(31) = "Albany"
        answers(32) = "Raleigh"
        answers(33) = "Bismarck"
        answers(34) = "Columbus"
        answers(35) = "Oklahoma City"
        answers(36) = "Salem"
        answers(37) = "Harrisburg"
        answers(38) = "Providence"
        answers(39) = "Columbia"
        answers(40) = "Pierre"
        answers(41) = "Nashville"
        answers(42) = "Austin"
        answers(43) = "Salt Lake City"
        answers(44) = "Montpelier"
        answers(45) = "Richmond"
        answers(46) = "Olympia"
        answers(47) = "Charleston"
        answers(48) = "Madison"
        answers(49) = "Cheyenne"

        For i = 0 To questions.Count - 1
            dtQAMain.Rows.Add(questions(i), answers(i)) 'assign QA in table for scoring purpose later
        Next
    End Sub
    Private Sub loadQsAndAs()
        Label1.Visible = False
        For i = 0 To alAnsButton.Count - 1
            alAnsButton(i).visible = True
        Next
        questionsCopy = New ArrayList
        questionsCopy = questions.Clone 'close a copy so we dont effect the actual question copy when randomize and eliminate asked question from arraylist
        TextBox1.Text = setTheQuestion()
        setTheAnswer()
    End Sub

    Private Function setTheQuestion() As String

        Dim randomValue As New Random
        Dim randomQ As String = ""
        Dim index As Integer
        If questionsCopy.Count <> 0 Then
            index = randomValue.Next(0, questionsCopy.Count - 1)
            randomQ = questionsCopy(index)
            questionsCopy.RemoveAt(index) 'asked question will be remove.

        Else ' questions are finished, show the mark
            ShowMark()
        End If

        Return randomQ

    End Function

    Private Sub setTheAnswer() 'randonmize the answer and assign to button

        If TextBox1.Text = "" Then Exit Sub ' if question finish exit sub
        Dim randomValue As New Random
        Dim NewIndex As Integer
        Dim temp As String
        Dim answersCopy As ArrayList = answers.Clone
        For n = answersCopy.Count - 1 To 0 Step -1
            NewIndex = randomValue.Next(0, n + 1)
            ' Swap them.
            temp = answersCopy(n)
            answersCopy(n) = answersCopy(NewIndex)
            answersCopy(NewIndex) = temp
        Next

        Dim AnswerRowCheck As Integer = questions.IndexOf(TextBox1.Text)
        Dim ActualAnswer As String = dtQAMain.Rows(AnswerRowCheck).Item("A") 'check actual answer
        Dim totalRemove As Integer = 0
        For i = answersCopy.Count - 1 To 0 Step -1
            If totalRemove = 2 Then Exit For
            If answersCopy(i) <> dtQAMain.Rows(AnswerRowCheck).Item("A") Then
                answersCopy.RemoveAt(i)
                totalRemove += 1
            End If
        Next 'remove 2 of the selection,since only 4 button for answer selection and should not take out the actual answer


        For i = 0 To alAnsButton.Count - 1
            alAnsButton(i).text = answersCopy(i)
        Next

    End Sub
    Private Sub ShowMark()
        For i = 0 To alAnsButton.Count - 1
            alAnsButton(i).text = "" 'clear the text, no more input receive from user.
        Next
        Label1.Visible = True
        Label1.Text = totalScore & " out of 50 are correct."
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        totalScore = 0
        loadQsAndAs() 'refresh question
    End Sub

    Private Sub Button4_MouseClick(sender As Object, e As MouseEventArgs) Handles Button4.MouseClick, Button3.MouseClick, Button2.MouseClick, Button1.MouseClick
        If sender.text = "" Then Exit Sub
        Dim AnswerRowCheck As Integer = questions.IndexOf(TextBox1.Text) 'search question number 
        If dtQAMain.Rows(AnswerRowCheck).Item("A") = sender.text Then 'checking answer 
            totalScore += 1
        End If
        TextBox1.Text = setTheQuestion() 'next question
        setTheAnswer()
    End Sub
End Class