You are on page 1of 2

How to Use Visual Basic to Create a Multiple Choice Test | Techwalla.com https://www.techwalla.com/articles/how-to-use-visual-basic-to-create-a-...

(/)

Home (/) /  Around The Home (/Around-the-home) /  Productivity (/Productivity)


/  How To Use Visual Basic To Create A Multiple Choice Test (/Articles/how-to-use-visual-basic-to-create-
a-multiple-choice-test)

How to Use Visual


Basic to Create a
Multiple Choice
Test
By Laura Gittins

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 RELATED
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 10 Tips to Photograph
form. the Essence of
Ad
Thanksgiving (/articles
/10-tips-to-capture-
PRODUCTIVITY (/P RODUCTIVITY)
Heat Transfer Liquid

paratherm.com

STEP 1
Open a new Visual Basic project. Add two
labels, three radio buttons and two button The 12 Most (and Least)
controls to the form. Drag the two labels to Family-Friendly
the top with Label1 on top of Label2. Drag Thanksgiving Movies
RadioButton1, RadioButton2 and RadioButton3 (/articles/the-12-
ENTERTAINMENT
to line up vertically beneath the labels. Drag
(/ENTERTAINMENT)
the two buttons beneath the radio buttons
with Button1 on the left and Button2 on the
right.

STEP 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 Techwalla's 2015 Holiday
dimension is for the question itself, three Buyers Guide (/articles
answer choices and the correct answer. The /techwallas-
second line creates an array to store the user's 2015-holiday-buyers-
PRODUCTIVITY (/P RODUCTIVITY)
answers. The third line creates a counter
variable that keeps track of the question the
user is on.

STEP 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"}, How to Find Your SSID
_{"What is the capital of Florida?", "Miami", Number (/articles
"Tallahassee", "Jacksonville", /how-to-find-your-
"Tallahassee"}}End SubThis subroutine simply ssid-number)
PRODUCTIVITY (/P RODUCTIVITY)
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.

STEP 4
Type the following code:Private Sub
MarkTest()Dim grade As Integer = 0For i = 0 How to Restore Your
To 2If answers(i) = questions(i, 4) Thengrade Computer's Settings to
+= 1End IfNextLabel1.Text = "Test an Earlier Date (/articles
finished!"Label2.Text = "You scored " & grade & /how-to-restore-
PRODUCTIVITY (/P RODUCTIVITY)
" 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.

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

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

STEP 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 < 3
ThenquesNum += 1Label1.Text = "Question "
& quesNum & " of " &
answers.LengthLabel2.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
ThenButton2.Text = "Submit"End
IfElseMarkTest()End IfThis 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.

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

Sponsore d Links (https://w ww.google .com /url?ct=abg&q=https: //www.googl e.c om /adse nse /support/bin/re que st.py %3 Fc ontact% 3Dabg_afc% 26url% 3Dhttps: //te chwalla.com /article s/how-to-us e-v isual-basic-to-cre ate-a-m ultiple -choice -te st% 26gl% 3DIN% 26hl% 3De n% 26clie nt% 3Dca-pub-0316 26511616 3263% 26ai0% 3DCqddrCfQ1WO_HMZe Tv gT71Yv ADtLT0KBH

D ownload
( h t t p s : / / w w w. g o o g l e a d s e r v i c e s . c o m
/ p a g e a d/ ac l k ? s a =L &
a i = C q d d r C f Q 1 W O _ H M Z e T v g T 7 1 Y v A D t LT 0 K B H 4 J Tc z _ c D x K a B 3 S I Q A S C c y e E E K A R g 5 d L k g 6 g O o A H x 7 _ b l A 8 g B A a g D A a o E v A F P 0 L C 9 p 5 O d 0 1 e g L j w 6 v M u U u 1 E a X A -
1 o YKp F MQ d g k C n z9 -
T Vb xZ LS R mu U b L -
m Q7 f 4 b g qh w0 O t izV mk V 4j -
y w 9 xOur_ _ Privacy
J d 8 - Policy (/privacy-policy) and Terms of Use (/terms-of-use) have been updated.
C SB E5 IYf KG T i5 O Z C 4 3 l QN N o 7 Kb u N mYG 7 Eg N N i iO AL r D 0 u9 I 2Q Bg h Z 7a qt F m XlY U oS bC V Vb 7 qL Dk w N4 Z d J 4q Gg r wXm 4 R J J 7 5s F Ey B 1c y r v 0 -

1 of 2 11/24/2016 1:26 AM
How to Use Visual Basic to Create a Multiple Choice Test | Techwalla.com https://www.techwalla.com/articles/how-to-use-visual-basic-to-create-a-...

(/)

 (HTTPS://WWW.FACEBOOK.COM/PAGES/TECHWALLA
/1116406528374498)  (HTTPS://TWITTER.COM
/TECHWALLAHQ)

HOW WE SCORE (/HOW-WE-SCORE) ABOUT US (/ABOUT) CONTACT US (/CONTACT) TERMS (/TERMS-OF-USE)

PRIVACY POLICY (/PRIVACY-POLICY) COPYRIGHT POLICY (/COPYRIGHT-POLICY) ADVERTISE (/ADVERTISE)

Enter email for updates


Our Privacy Policy (/privacy-policy) and Terms of Use (/terms-of-use) have been updated.

2 of 2 11/24/2016 1:26 AM

You might also like