Blue Gold Basics Sae Programs Multiple Choice
- Remove From My Forums
Visual Basic Multiple Choice Project
-
Question
-
Can someone please help me with this code? I'm trying to write a program to simulate a multiple choice test but it doesn't seem to work when I debug.
Public Class Form1
Dim questions(2, 4) As String
Dim answers(2) As String
Dim quesNum As Integer
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
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 SubPrivate Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
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
End SubPrivate Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
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
End Sub
End Class
Answers
-
I don't particularly like dealing with multi-dimensional arrays. Have you considered making the quiz and its questions into a class. Here's an example, using lists instead of arrays.
Simply start a new project(for testing), then highlight all of form1's code, and paste this over it. Then run the project. I have made it where the form will draw itself.
If you like this code, you can modify it to your needs.
Option Strict On
Public Class Form1
Dim TestAboutMe As New Quiz
Friend WithEvents QuizStart, AnswerSubmit As New System.Windows.Forms.Button
Friend WithEvents RadioButton1, RadioButton2, RadioButton3, RadioButton4 As New System.Windows.Forms.RadioButton
Friend WithEvents Label1 As New System.Windows.Forms.Label
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.Width = 297 : Me.Height = 229 : Label1.Top = 0
Label1.Text = "Click 'Start Quiz' to begin the quiz."
Label1.Width = Me.Width
RadioButton1.Top = Label1.Top + Label1.Height
RadioButton2.Top = RadioButton1.Top + RadioButton1.Height
RadioButton3.Top = RadioButton2.Top + RadioButton2.Height
RadioButton4.Top = RadioButton3.Top + RadioButton3.Height
RadioButton1.Width = Me.Width : RadioButton2.Width = Me.Width
RadioButton3.Width = Me.Width : RadioButton4.Width = Me.Width
RadioButton1.Checked = False : RadioButton2.Checked = False
RadioButton3.Checked = False : RadioButton4.Checked = False
RadioButton1.Visible = False : RadioButton2.Visible = False
RadioButton3.Visible = False : RadioButton4.Visible = False
AnswerSubmit.Visible = False
AnswerSubmit.Top = Me.ClientRectangle.Height - AnswerSubmit.ClientRectangle.Height
QuizStart.Top = Me.ClientRectangle.Height - QuizStart.ClientRectangle.Height
QuizStart.Text = "Start Quiz" : AnswerSubmit.Text = "Submit Answer"
Me.Controls.Add(AnswerSubmit) : Me.Controls.Add(QuizStart)
Me.Controls.Add(RadioButton1) : Me.Controls.Add(RadioButton2)
Me.Controls.Add(RadioButton3) : Me.Controls.Add(RadioButton4)
Me.Controls.Add(Label1) : AnswerSubmit.Visible = False
AddHandler QuizStart.Click, AddressOf QuizStart_Click
AddHandler AnswerSubmit.Click, AddressOf AnswerSubmit_Click
End Sub
Private Sub QuizStart_Click(sender As System.Object, e As System.EventArgs)
ResetQuiz() : UpdateQuestion() : QuizStart.Visible = False : AnswerSubmit.Visible = True
End Sub
Private Sub AnswerSubmit_Click(sender As System.Object, e As System.EventArgs)
Dim Answer As Integer = 0
If RadioButton1.Checked = True Then Answer = 1
If RadioButton2.Checked = True Then Answer = 2
If RadioButton3.Checked = True Then Answer = 3
If RadioButton4.Checked = True Then Answer = 4
Select Case Answer
Case 1 : TestAboutMe.GradeQuestion(RadioButton1.Text)
UpdateQuestion()
Case 2 : TestAboutMe.GradeQuestion(RadioButton2.Text)
UpdateQuestion()
Case 3 : TestAboutMe.GradeQuestion(RadioButton3.Text)
UpdateQuestion()
Case 4 : TestAboutMe.GradeQuestion(RadioButton4.Text)
UpdateQuestion()
Case Else : MsgBox("You must check an answer!")
End Select
Select Case TestAboutMe.TestComplete
Case True
RadioButton1.Text = "" : RadioButton2.Text = ""
RadioButton3.Text = "" : RadioButton4.Text = ""
Label1.Text = Math.Round((TestAboutMe.CorrectAnswers / TestAboutMe.TotalQuestions) * 100, 2) & "% You got " & TestAboutMe.CorrectAnswers & " question(s) out of " & TestAboutMe.TotalQuestions & " right."
RadioButton1.Checked = False : RadioButton2.Checked = False
RadioButton3.Checked = False : RadioButton4.Checked = False
RadioButton1.Visible = False : RadioButton2.Visible = False
RadioButton3.Visible = False : RadioButton4.Visible = False
AnswerSubmit.Visible = False : QuizStart.Visible = True
Case Else
RadioButton1.Checked = False : RadioButton2.Checked = False
RadioButton3.Checked = False : RadioButton4.Checked = False
End Select
End Sub
Sub ResetQuiz()
TestAboutMe.Clear()
TestAboutMe.AddQuestion(New Quiz.Question("What is my favorite color?", {"red", "blue", "green", "orange"}.ToList, "blue"))
TestAboutMe.AddQuestion(New Quiz.Question("Pick a number between 1 and 9.", {"1", "3", "6", "9"}.ToList, "3"))
TestAboutMe.AddQuestion(New Quiz.Question("What is my name?", {"Paul", "Armin", "Devon", "ctsang11"}.ToList, "Paul"))
RadioButton1.Checked = False : RadioButton2.Checked = False
RadioButton3.Checked = False : RadioButton4.Checked = False
RadioButton1.Visible = True : RadioButton2.Visible = True
RadioButton3.Visible = True : RadioButton4.Visible = True
End Sub
Sub UpdateQuestion()
TestAboutMe.NextQuestion()
Label1.Text = TestAboutMe.CurrentQuestion.Question
RadioButton1.Text = TestAboutMe.CurrentQuestion.Choices(0)
RadioButton2.Text = TestAboutMe.CurrentQuestion.Choices(1)
RadioButton3.Text = TestAboutMe.CurrentQuestion.Choices(2)
RadioButton4.Text = TestAboutMe.CurrentQuestion.Choices(3)
End Sub
End Class
Public Class Quiz
Private Property _Questions As New List(Of Question)
Private Index As Integer = 0
Private _CurrentQuestion As Question
Private Property _CorrectAnswers As Integer = 0
Private Property _TestComplete As Boolean = False
Public ReadOnly Property TestComplete As Boolean
Get
Return _TestComplete
End Get
End PropertyPublic ReadOnly Property CurrentQuestion As Question
Get
Return _CurrentQuestion
End Get
End Property
Public ReadOnly Property TotalQuestions As Integer
Get
Return _Questions.Count
End Get
End Property
Public ReadOnly Property CorrectAnswers As Integer
Get
Return _CorrectAnswers
End Get
End Property
Public ReadOnly Property Questions As List(Of Question)
Get
Return _Questions
End Get
End Property
Public Sub NextQuestion()
Dim TmpIndex As Integer = Index
Index = Index + 1
If TmpIndex > Questions.Count - 1 Then
_TestComplete = True
Exit Sub
End If
_CurrentQuestion = Questions(TmpIndex)
End Sub
Public Sub AddQuestion(ByVal Question As Question)
_Questions.Add(Question)
End Sub
Public Sub GradeQuestion(ChosenAnswer As String)
If ChosenAnswer = CurrentQuestion.CorrectAnswer Then
_CorrectAnswers = _CorrectAnswers + 1
End If
End Sub
Public Sub Clear()
_Questions.Clear()
Index = 0
_CorrectAnswers = 0
_TestComplete = False
End Sub
Class Question
Private _Question As String
Private _Choices As New List(Of String)
Private _CorrectAnswer As String
Public ReadOnly Property Question As String
Get
Return _Question
End Get
End Property
Public ReadOnly Property Choices As List(Of String)
Get
Return _Choices
End Get
End Property
Public ReadOnly Property CorrectAnswer As String
Get
Return _CorrectAnswer
End Get
End Property
Sub New(Question As String, Choices As List(Of String), CorrectAnswer As String)
_Question = Question
_Choices = Choices
_CorrectAnswer = CorrectAnswer
End Sub
End Class
End Class
If you want something you've never had, you need to do something you've never done. If you believe something to be true, then one day you will be called upon to demonstrate that truth.
- Edited by Wednesday, July 25, 2012 5:02 AM
- Marked as answer by Youen Zen Moderator Friday, August 3, 2012 9:08 AM
Source: https://social.msdn.microsoft.com/Forums/en-US/86e25b73-1190-4482-a335-21b87f863d26/visual-basic-multiple-choice-project?forum=Vsexpressvb
0 Response to "Blue Gold Basics Sae Programs Multiple Choice"
Post a Comment