We Value Your Privacy

We and our partners use technology such as cookies on our site to personalise content and ads, provide social media features, and analyse our traffic. Click below to consent to the use of this technology across the web. You can change your mind and change your consent choices at anytime by returning to this site.

Update Consent
Loading ...

How to use visual basic to create a multiple choice test

Updated March 23, 2017

Each question in a multiple choice test consists of a question itself and several different answers, only one of which is correct. You can create a multiple choice test in Visual Basic using labels to display the questions, radio buttons for the possible answers and button controls to navigate between different questions. In the code, use arrays to save the questions and answers that the user submits and maintain a counter variable to keep track of what question the user is on. At the end of the test, mark the user's answers and display the result on the form.

Loading ...
  1. Open a new Visual Basic project. Add two labels, three radio buttons and two button controls to the form. Drag the two labels to the top with Label1 on top of Label2. Drag RadioButton1, RadioButton2 and RadioButton3 to line up vertically beneath the labels. Drag the two buttons beneath the radio buttons with Button1 on the left and Button2 on the right.

  2. Press "F7" to open the code window. Type the following code at the class level:

  3. Dim questions(2, 4) As String

  4. Dim answers(2) As String

  5. Dim quesNum As Integer

  6. The first line creates a two-dimensional array. The first dimension is for each question and the second dimension is for the question itself, three answer choices and the correct answer. The second line creates an array to store the user's answers. The third line creates a counter variable that keeps track of the question the user is on.

  7. Type the following code:

  8. Private Sub GetQuestions()

  9. questions = New String(,) {{"How many colours are in a rainbow?", "5", "6", "7", "7"}, _

  10. {"Who starred in Pirates of the Caribbean?", "Johnny Depp", "John Malkovich", "John Cusack", "Johnny Depp"}, _

  11. {"What is the capital of Florida?", "Miami", "Tallahassee", "Jacksonville", "Tallahassee"}}

  12. End Sub

  13. This subroutine simply initialises the three questions and answers in the questions array. You can add additional questions or get them in other ways, such as through a text file, but if you do, remember to change the size of the question and answer arrays to accommodate the number of questions.

  14. Type the following code:

  15. Private Sub MarkTest()

  16. Dim grade As Integer = 0

  17. For i = 0 To 2

  18. If answers(i) = questions(i, 4) Then

  19. grade += 1

  20. End If

  21. Next

  22. Label1.Text = "Test finished!"

  23. Label2.Text = "You scored " & grade & " out of " & answers.Length & "!"

  24. RadioButton1.Enabled = False

  25. RadioButton2.Enabled = False

  26. RadioButton3.Enabled = False

  27. Button1.Enabled = False

  28. Button2.Enabled = False

  29. End Sub

  30. The first line declares a subroutine that marks the test. It creates a local variable to count the score, then cycles through the answers in the questions array and the answers submitted by the user. For each answer that matches, the grade goes up by one. It then displays the score in the labels and disables the rest of the controls.

  31. Open the Form1_Load() subroutine and type the following code:

  32. Me.Text = "My Multiple Choice Quiz!"

  33. GetQuestions()

  34. quesNum = 1

  35. Label1.Text = "Question " & quesNum & " of " & answers.Length

  36. Label2.Text = questions(0, 0)

  37. Button1.Text = "Previous"

  38. Button2.Text = "Next"

  39. RadioButton1.Text = questions(0, 1)

  40. RadioButton2.Text = questions(0, 2)

  41. RadioButton3.Text = questions(0, 3)

  42. The first line sets the title in the title bar. The next line calls the GetQuestions() subroutine. The third line initialises the question counter variable. The fourth line displays what question number the user is on. The fifth line displays question one in the label. The sixth and seventh lines change the text for the two buttons. The last three lines insert the three multiple choice answers as text for the three radio buttons.

  43. Open the Button1_Click() subroutine and type the following code:

  44. If quesNum > 1 Then

  45. quesNum -= 1

  46. Label1.Text = "Question " & quesNum & " of 3"

  47. Label2.Text = questions(quesNum - 1, 0)

  48. RadioButton1.Text = questions(quesNum - 1, 1)

  49. RadioButton2.Text = questions(quesNum - 1, 2)

  50. RadioButton3.Text = questions(quesNum - 1, 3)

  51. If Button2.Text = "Submit" Then

  52. Button2.Text = "Next"

  53. End If

  54. End If

  55. This is the code for the "Previous" button. It first checks to see if the user pressed the button while already on the first question. If not, it decrements the question counter by one and updates the text for the labels and radio buttons to show the previous question. If the user was on the final question, the text on Button2 changes from "Submit" back to "Next."

  56. Open the Button2_Click() subroutine and type the following code:

  57. If RadioButton1.Checked = True Then

  58. answers(quesNum - 1) = RadioButton1.Text

  59. ElseIf RadioButton2.Checked = True Then

  60. answers(quesNum - 1) = RadioButton2.Text

  61. ElseIf RadioButton3.Checked = True Then

  62. answers(quesNum - 1) = RadioButton3.Text

  63. End If

  64. RadioButton1.Focus()

  65. If quesNum < 3 Then

  66. quesNum += 1

  67. Label1.Text = "Question " & quesNum & " of " & answers.Length

  68. Label2.Text = questions(quesNum - 1, 0)

  69. RadioButton1.Text = questions(quesNum - 1, 1)

  70. RadioButton2.Text = questions(quesNum - 1, 2)

  71. RadioButton3.Text = questions(quesNum - 1, 3)

  72. If quesNum = 3 Then

  73. Button2.Text = "Submit"

  74. End If

  75. Else

  76. MarkTest()

  77. End If

  78. This is the code for the "Next" button. The first seven lines check to see what radio button the user had selected, then saves that answer to the answers array. The next line focuses the radio button selection on RadioButton1. The next line checks to see that the user is not on the final question. If this is true, it increases the question counter by one and updates the labels and radio buttons to show the next question. It then checks to see if the user is now on the final question. If so, it changes the text for the Next button from "Next" to "Submit." If the user was already on the final question and clicked "Submit," the program calls the "MarkTest" function to get the user's score.

  79. Save the Visual Basic program. Press "F5" to run it.

Loading ...

About the Author

Laura Gittins has been writing since 2008 and is an expert in document design. She has a Bachelor of Science in English, Professional and Technical Writing. She has written education and document design articles for eHow.

Loading ...