Brothersoft.comWindows|Mac|Mobile|Games

|Message

Solve software problem quickly Share experience to help others Improve the ability of solving problem
How to Use Visual Basic to Create a Multiple Choice Test?

11/02/2011 10:57 by SaideeDay

First answer posted by PollyP at 11/02/2011 10:57
Add Your Answer
10~1000 characters in length CAPTCHA:
7 Answers
  • PollyP
    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:

      Dim questions(2, 4) As String

      Dim answers(2) As String

      Dim quesNum As Integer

      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.

    3. Type the following code:

      Private Sub GetQuestions()

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

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

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

      End Sub

      This subroutine simply initializes 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.

    4. Type the following code:

      Private Sub MarkTest()

      Dim grade As Integer = 0

      For i = 0 To 2

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

      grade += 1

      End If

      Next

      Label1.Text = "Test finished!"

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

      RadioButton1.Enabled = False

      RadioButton2.Enabled = False

      RadioButton3.Enabled = False

      Button1.Enabled = False

      Button2.Enabled = False

      End Sub

      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.

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

      Me.Text = "My Multiple Choice Quiz!"

      GetQuestions()

      quesNum = 1

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

      Label2.Text = questions(0, 0)

      Button1.Text = "Previous"

      Button2.Text = "Next"

      RadioButton1.Text = questions(0, 1)

      RadioButton2.Text = questions(0, 2)

      RadioButton3.Text = questions(0, 3)

      The first line sets the title in the title bar. The next line calls the GetQuestions() subroutine. The third line initializes 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.

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

      If quesNum > 1 Then

      quesNum -= 1

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

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

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

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

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

      If Button2.Text = "Submit" Then

      Button2.Text = "Next"

      End If

      End If

      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."

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

      If RadioButton1.Checked = True Then

      answers(quesNum - 1) = RadioButton1.Text

      ElseIf RadioButton2.Checked = True Then

      answers(quesNum - 1) = RadioButton2.Text

      ElseIf RadioButton3.Checked = True Then

      answers(quesNum - 1) = RadioButton3.Text

      End If

      RadioButton1.Focus()

      If quesNum < 3 Then

      quesNum += 1

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

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

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

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

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

      If quesNum = 3 Then

      Button2.Text = "Submit"

      End If

      Else

      MarkTest()

      End If

      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.

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

  • Was this answer helpful? 00 · 11/02/2011 10:57
  • Anonymous user
  • a ok.. tahnk you.. 
  • Was this answer helpful? 00 · 09/08/2014 07:38
  • Anonymous user
  • Hello, how can we generate random questions? Thanks
  • Was this answer helpful? 00 · 09/28/2014 03:19
  • Anonymous user
  • pjk[ok[ok[[pk
  • Was this answer helpful? 00 · 04/01/2016 06:14
  • Anonymous user
  • uguirie;r9oc68o79
  • Was this answer helpful? 00 · 06/10/2016 11:01
  • Anonymous user
  • um i dont get how to add more than 3 ques. :(
  • Was this answer helpful? 00 · 06/26/2016 21:02
  • Anonymous user
  • hi,  how can we generate random questions? thank you
  • Was this answer helpful? 00 · 04/06/2017 15:38
Add Your Answer
10~1000 characters in length CAPTCHA:

Added Successfully!

×

Are you sure to delete your answer?

NoYes

×

Are you sure to choose it as the best answer?

NoYes

×

Voted Successfully!

×

You can't vote for yourself

×

You can't choose your own answer

×