Question:
RE: How to make a test in Visual Basic?
Nobody
2013-02-08 13:34:26 UTC
Alright I'm making a program that will display a proverb in the textbox, allow the user to check True or False radio button, and then click next to record if they were right or not and show the next proverb. When they've answered all the questions how many they got right should be displayed in a different textbox as a number and an expression should show in another textbox based on how many right they got (IE "Excellent" if all right, "Good" if 5-6 right)

Here is my code so far.


Public Class frmProverbsTest
Dim i As Integer
Dim Result As String = Nothing
Dim Total As Integer = 0
Dim Questions() As String = {"The squeaky wheel gets the grease", "Cry and you cry alone", "Opposites attract", "Spare the rod and spoil the child", "Actions speak louder than words", "Familiarity breeds contempt", "Marry in haste, repent at leisure"}
Dim ans() As Boolean = {True, True, False, False, True, False, True}
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Dim i As Integer
txtbox1.Text = Questions(i)
For i = 0 To 6
If rbTrue.Checked = True And ans(i) = True Then
Total += 1
End If
If rbFalse.Checked = True And ans(i) = False Then
Total += 1
End If
Next
End Sub

Private Sub btnScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScore.Click
Select Case Total
Case Total < 5
Result = "You might consider taking Psychology 101"
Case Total = 5, 6
Result = "Excellent"
Case Total = 7
Result = "Perfect"
End Select
txtboxResult.Text = Result
txtboxCorrect.Text = i
End Sub
End Class

My first problem is that the textbox isn't displaying the Questions. It only displays one, then when you hit next nothing happens. Also when I click Done it doesn't give a Result, it is left blank and # correct is always 0.

So how can I fix this? Am I going about it the right way at least? Thanks in advance.
Three answers:
Dani
2013-02-08 14:27:13 UTC
Try running the program in debug mode. When you do this the computer automaticly checks for bugs in the code, and will then tell you them upon exiting the program.
Jeremy
2013-02-08 13:52:57 UTC
There are some problems. Your next button is getting the answers for ALL of the questions. That means, the quiz is allowing the user to put down one answer. Once he clicks next, it loops through all of them with that answer, no giving him a chance to change it for question 2, 3, 4, and so on.



Also, your scoring is a bit wrong. You're comparing a numeric value to a boolean one. Try using "Select Case True" instead. If your condition is true, like Total < 5, it will fall into the appropriate spot.



Here is some modified code:



Public Class Form2

Private Questions() As String = {"The squeaky wheel gets the grease", "Cry and you cry alone", "Opposites attract", "Spare the rod and spoil the child", "Actions speak louder than words", "Familiarity breeds contempt", "Marry in haste, repent at leisure"}

Private ans() As Boolean = {True, True, False, False, True, False, True}

Private Total As Integer = 0

Private currentQuestion As Integer = 0

Private Sub Form2_Load(sender As Object, e As System.EventArgs) Handles Me.Load

rbTrue.Checked = True

ShowQuestion()

End Sub

Private Sub ShowQuestion()

txtQuestion.Text = Questions(currentQuestion)

End Sub

Private Sub ShowScore()

Select Case True

Case Total < 5

txtboxResult.Text = "You might consider taking Psychology 101"

Case Total = 5, Total = 6

txtboxResult.Text = "Excellent"

Case Total = 7

txtboxResult.Text = "Perfect"

End Select

txtBoxCorrect.Text = CStr(Total)

End Sub

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click

If rbTrue.Checked = ans(currentQuestion) Then

Total += 1

End If

If currentQuestion = Questions.Length - 1 Then

ShowScore()

btnNext.Enabled = False

Else

currentQuestion += 1

ShowQuestion()

End If

End Sub

End Class
2013-02-12 06:39:14 UTC
Ture!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...