Tuesday, April 7, 2015

How to Use Visual Basic to Create a Multiple Choice Test

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 StringDim answers(2) As StringDim quesNum As IntegerThe 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 SubThis 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 = 0For i = 0 To 2If answers(i) = questions(i, 4) Thengrade += 1End IfNextLabel1.Text = 'Test finished!'Label2.Text = 'You scored ' & grade & ' out of ' & answers.Length & '!'RadioButton1.Enabled = FalseRadioButton2.Enabled = FalseRadioButton3.Enabled = FalseButton1.Enabled = FalseButton2.Enabled = FalseEnd SubThe 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 = 1Label1.Text = 'Question ' & quesNum & ' of ' & answers.LengthLabel2.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 ThenquesNum -= 1Label1.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' ThenButton2.Text = 'Next'End IfEnd IfThis 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 Thenanswers(quesNum - 1) = RadioButton1.TextElseIf RadioButton2.Checked = True Thenanswers(quesNum - 1) = RadioButton2.TextElseIf RadioButton3.Checked = True Thenanswers(quesNum - 1) = RadioButton3.TextEnd IfRadioButton1.Focus()If quesNum
8. Save the Visual Basic program. Press 'F5' to run it.

No comments:

Post a Comment