none
Quiz Application RRS feed

  • Question

  • Hi All

    I am needing to create a "quiz" application - basically something to have ability to show a number of multiple choice questions which the user can answer and then at the end it shows results etc on screen

    Some questions would be simply True/False, some would be select two answers from list below, some would be select all that apply from list below

    Just wanted to get thoughts on best way to achieve this so in future I can easily add more questions to it, rather than say coding each question separately with if answer = this then do that etc

    All input gratefully received as always :)


    Darren Rose

    Wednesday, June 27, 2018 5:24 PM

Answers

  • Hi

    Here is some code as a stand alone project that may help you get started. It is really very basic with no frills at all, and very limited error checking. This example just needs a blank default Form1.

    ' Blank Form1 - all controls are
    ' generated at run time.
    
    ' ==============================
    ' Test text file contains:
    ' ==============================
    
    '// format: QUESTION "|"  any number of Answers comma separated "#" which correct answer number
    
    ' How many fingers am I holding up?|7,1,11,4#3
    ' What Is my favourite colour?|Red, Green, Blue, Transparent#4
    ' Which Is the correct side Of the road To drive On?|Left, Right, Middle#1
    ' Can a cat fly?|Yes,No,only If I Throw it#3
    ' Am I a brilliant coder?|Yes,No,Maybe,Not On your life#4
    
    Option Strict On
    Option Explicit On
    Public Class Form1
      'Path to textfile questions/answers
      Private Path As String = Application.StartupPath & "\Data\LearntWords.txt"
      Private Questions As New List(Of Question)
      Private Rand As New Random
      Private QtextBox As New TextBox
      Private butt As New Button
      Private Panel As New Panel
      Private RightWrong As New Label
      Private UserAnswers As New List(Of UserInput)
      Private UsedID As New List(Of Integer)
      Private Lab1, lab2, lab3, lab4 As New Label
      Sub GetQuestionsFromFile()
        Dim i As Integer = 1
        Using fn As IO.StreamReader = New IO.StreamReader(Path)
          Do
            Dim line As String = fn.ReadLine
            If Not line = Nothing AndAlso Not line.StartsWith("//") Then
              Dim q As New Question
              Dim a() As String = line.Split("|"c)
              Dim b() As String = a(1).Split("#"c)
              Dim c() As String = b(0).Split(","c)
              With q
                .ID = i
                .Quest = a(0)
                .Choices = c.ToList
                .Answer = GetInteger(b(1))
              End With
              Questions.Add(q)
              i += 1
            End If
          Loop Until fn.EndOfStream
        End Using
      End Sub
      Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load
    
        GetQuestionsFromFile()
    
        ' set up controls
        Size = New Size(500, 400)
        With QtextBox
          .Font = New Font("Arial", 14)
          .Width = ClientSize.Width - 30
          .Location = New Point(20, 20)
          .Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
        End With
        With Panel
          .Location = New Point(20, QtextBox.Bottom + 20)
          .Width = ClientSize.Width - 130
          .Height = ClientSize.Height - 130
          .BorderStyle = BorderStyle.FixedSingle
          .Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Bottom
        End With
        With butt
          .Text = "Next Question"
          .AutoSize = True
          .Location = New Point(20, Panel.Bottom + 20)
          .Anchor = AnchorStyles.Left Or AnchorStyles.Bottom
        End With
        With RightWrong
          .Text = "Click button for Next Question"
          .Location = New Point(butt.Right + 28, butt.Top)
          .Font = New Font("Arial", 14)
          .AutoSize = True
        End With
        With Lab1
          .Text = "Right"
          .Location = New Point(Panel.Right + 18, Panel.Top)
          .Anchor = AnchorStyles.Right Or AnchorStyles.Top
        End With
        With lab2
          .Text = "Wrong"
          .Location = New Point(Panel.Right + 18, Panel.Top + 100)
          .Font = New Font("Arial", 14)
          .Anchor = AnchorStyles.Right Or AnchorStyles.Top
        End With
        With lab3
          .Text = "0"
          .Location = New Point(Panel.Right + 20, Panel.Top + 30)
          .Font = New Font("Arial", 24)
          .AutoSize = True
          .Anchor = AnchorStyles.Right Or AnchorStyles.Top
        End With
        With lab4
          .Text = "0"
          .Location = New Point(Panel.Right + 20, Panel.Top + 130)
          .Font = New Font("Arial", 24)
          .AutoSize = True
          .Anchor = AnchorStyles.Right Or AnchorStyles.Top
        End With
        Controls.AddRange({QtextBox, Panel, butt, RightWrong, Lab1, lab2, lab3, lab4})
        AddHandler butt.Click, AddressOf NewQuest
      End Sub
      Private Sub NewQuest(sender As Object, e As EventArgs)
        Static FirstQuestion As Boolean = True
        Static indx As Integer = 0
        If butt.Text = "New Game" Then
          RightWrong.Text = "Click button for Next Question"
          butt.Text = "Next Question"
          lab3.Text = "0"
          lab4.Text = "0"
          UsedID.Clear()
          Exit Sub
        End If
        If Not FirstQuestion Then
          Dim User As New UserInput With {.Quest = QtextBox.Text, .Answer = FindUserInput()}
          UserAnswers.Add(User)
          If User.Answer = Questions(indx).Answer Then
            lab3.Text = (GetInteger(lab3.Text) + 1).ToString
          Else
            lab4.Text = (GetInteger(lab4.Text) + 1).ToString
          End If
          If UsedID.Count >= Questions.Count Then
            RightWrong.Text = "Game Over with a Score of " & lab3.Text & " out of " & Questions.Count.ToString
            butt.Text = "New Game"
            Exit Sub
          End If
        End If
        Do
          indx = Rand.Next(0, Questions.Count)
        Loop While UsedID.Contains(indx)
        UsedID.Add(indx)
        QtextBox.Text = Questions(indx).Quest
        Dim y As Integer = 4
        Dim tg As Integer = 1
        Panel.Controls.Clear()
        For Each s As String In Questions(indx).Choices
          Dim rb As New RadioButton With {.Text = s, .Location = New Point(8, y), .Tag = tg}
          Panel.Controls.Add(rb)
          y += 20
          tg += 1
        Next
        FirstQuestion = False
      End Sub
      Private Function FindUserInput() As Integer
        Dim answer As Integer = -1
        For Each c As Control In Me.Panel.Controls
          If c.GetType() Is GetType(RadioButton) Then
            Dim rb As RadioButton = DirectCast(c, RadioButton)
            If rb.Checked Then
              answer = CInt(rb.Tag)
            End If
          End If
        Next
        Return answer
      End Function
      Private Function GetInteger(s As String) As Integer
        Dim v As Integer = 0
        If Integer.TryParse(s, v) Then Return v
        Return 0
      End Function
      Class Question
        Property ID As Integer
        Property Quest As String
        Property Choices As List(Of String)
        Property Answer As Integer
      End Class
      Class UserInput
        Property Quest As String
        Property Answer As Integer
      End Class
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by wingers Thursday, June 28, 2018 4:54 PM
    Wednesday, June 27, 2018 5:41 PM
  • I would use an XML file to define the quiz so that you can write one code solution that can handle both types of questions for any size quiz.

    Here is a simple example.  In the real project you might encrypt the quiz file or save it as an embedded resource.  You could also add a class to keep track of results rather than just showing a correct/incorrect response.  But this should get you started and be easy to expand upon.

    <?xml version="1.0" encoding="utf-8" ?>
    <Quiz>
      <Question type="TrueFalse" answer="True">
        A whale is the largest marine mammal.
      </Question>
      <Question type="Multiple" answer="B">
        What is the largest marine mammal?
        <Choice letter="A" value="Elephant" />
        <Choice letter="B" value="Whale" />
        <Choice letter="C" value="Shark" />
        <Choice letter="D" value="Loch Ness Monster" />
      </Question>
      <Question type="Multiple" answer="E">
        Which of these are part of a computer?
        <Choice letter="A" value="CPU" />
        <Choice letter="B" value="RAM" />
        <Choice letter="C" value="Hard Drive" />
        <Choice letter="D" value="Motherboard" />
        <Choice letter="E" value="All of the above" />
      </Question>
    </Quiz>

    Code:

    Public Class Form1
        Private QuestionPanel As New Panel With {.Dock = DockStyle.Fill}
        Private WithEvents NextButton As New Button With {.Dock = DockStyle.Bottom, .Text = "Next Question"}
        Private YesNoPanel As New FlowLayoutPanel With {.Dock = DockStyle.Fill, .Name = "YesNoPanel"}
        Private MultiPanel As New FlowLayoutPanel With {.Dock = DockStyle.Fill, .Name = "MultiPanel"}
        Private YesNoQuestion As New Label With {.AutoSize = True}
        Private MultiQuestion As New Label With {.AutoSize = True}
        Private YesButton As New RadioButton With {.AutoSize = True, .Text = "True"}
        Private NoButton As New RadioButton With {.AutoSize = True, .Text = "False"}
        Private ChoiceA As New RadioButton With {.AutoSize = True, .Name = "ChoiceA"}
        Private ChoiceB As New RadioButton With {.AutoSize = True, .Name = "ChoiceB"}
        Private ChoiceC As New RadioButton With {.AutoSize = True, .Name = "ChoiceC"}
        Private ChoiceD As New RadioButton With {.AutoSize = True, .Name = "ChoiceD"}
        Private ChoiceE As New RadioButton With {.AutoSize = True, .Name = "ChoiceE"}
        Private ChoiceF As New RadioButton With {.AutoSize = True, .Name = "ChoiceF"}
    
        Private QuizDoc As XDocument
        Private QuestionNumber As Integer
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With YesNoPanel
                .Controls.Add(YesNoQuestion)
                .SetFlowBreak(YesNoQuestion, True)
                .Controls.Add(YesButton)
                .SetFlowBreak(YesButton, True)
                .Controls.Add(NoButton)
            End With
            With MultiPanel
                .Controls.Add(MultiQuestion)
                .SetFlowBreak(MultiQuestion, True)
                .Controls.Add(ChoiceA)
                .SetFlowBreak(ChoiceA, True)
                .Controls.Add(ChoiceB)
                .SetFlowBreak(ChoiceB, True)
                .Controls.Add(ChoiceC)
                .SetFlowBreak(ChoiceC, True)
                .Controls.Add(ChoiceD)
                .SetFlowBreak(ChoiceD, True)
                .Controls.Add(ChoiceE)
                .SetFlowBreak(ChoiceE, True)
                .Controls.Add(ChoiceF)
            End With
            Controls.Add(NextButton)
            Controls.Add(QuestionPanel)
    
            QuizDoc = XDocument.Load("Quiz1.xml")
            ShowNextQuestion()
        End Sub
    
        Private Sub LoadYesNoQuestion(question As XElement)
            If Not QuestionPanel.Controls.ContainsKey("YesNoPanel") Then
                QuestionPanel.Controls.Clear()
                QuestionPanel.Controls.Add(YesNoPanel)
            End If
            YesNoQuestion.Text = question.Value
            YesButton.Checked = False
            NoButton.Checked = False
            If question.@answer = "True" Then
                YesButton.Tag = True
                NoButton.Tag = False
            Else
                YesButton.Tag = False
                NoButton.Tag = True
            End If
        End Sub
    
        Private Sub LoadMultiQuestion(question As XElement)
            If Not QuestionPanel.Controls.ContainsKey("MultiPanel") Then
                QuestionPanel.Controls.Clear()
                QuestionPanel.Controls.Add(MultiPanel)
            End If
            MultiQuestion.Text = question.Value
            For Each ctrl In MultiPanel.Controls.OfType(Of RadioButton)
                ctrl.Checked = False
                ctrl.Visible = False
            Next
            For Each choice In question.<Choice>
                Dim ctrl = MultiPanel.Controls.Item("Choice" & choice.@letter)
                ctrl.Visible = True
                ctrl.Text = choice.@value
                If choice.@letter = question.@answer Then
                    ctrl.Tag = True
                Else
                    ctrl.Tag = False
                End If
            Next
        End Sub
    
        Private Sub ShowNextQuestion()
            If QuestionNumber < QuizDoc.Root.<Question>.Count Then
                Dim question = QuizDoc.Root.<Question>.ElementAt(QuestionNumber)
                If question.@type = "TrueFalse" Then
                    LoadYesNoQuestion(question)
                Else
                    LoadMultiQuestion(question)
                End If
                QuestionNumber += 1
            Else
                MessageBox.Show("You have reached the end of the quiz.")
            End If
        End Sub
    
        Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click
            Dim checkedAnswer = (From ctrl In QuestionPanel.Controls(0).Controls.OfType(Of RadioButton) Where ctrl.Checked = True).First
            If CBool(checkedAnswer.Tag) = True Then
                MessageBox.Show("Correct!")
            Else
                MessageBox.Show("Sorry, that is not correct.")
            End If
            ShowNextQuestion()
        End Sub
    End Class
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by wingers Thursday, June 28, 2018 4:54 PM
    Wednesday, June 27, 2018 6:45 PM
    Moderator

All replies

  • Hi

    Here is some code as a stand alone project that may help you get started. It is really very basic with no frills at all, and very limited error checking. This example just needs a blank default Form1.

    ' Blank Form1 - all controls are
    ' generated at run time.
    
    ' ==============================
    ' Test text file contains:
    ' ==============================
    
    '// format: QUESTION "|"  any number of Answers comma separated "#" which correct answer number
    
    ' How many fingers am I holding up?|7,1,11,4#3
    ' What Is my favourite colour?|Red, Green, Blue, Transparent#4
    ' Which Is the correct side Of the road To drive On?|Left, Right, Middle#1
    ' Can a cat fly?|Yes,No,only If I Throw it#3
    ' Am I a brilliant coder?|Yes,No,Maybe,Not On your life#4
    
    Option Strict On
    Option Explicit On
    Public Class Form1
      'Path to textfile questions/answers
      Private Path As String = Application.StartupPath & "\Data\LearntWords.txt"
      Private Questions As New List(Of Question)
      Private Rand As New Random
      Private QtextBox As New TextBox
      Private butt As New Button
      Private Panel As New Panel
      Private RightWrong As New Label
      Private UserAnswers As New List(Of UserInput)
      Private UsedID As New List(Of Integer)
      Private Lab1, lab2, lab3, lab4 As New Label
      Sub GetQuestionsFromFile()
        Dim i As Integer = 1
        Using fn As IO.StreamReader = New IO.StreamReader(Path)
          Do
            Dim line As String = fn.ReadLine
            If Not line = Nothing AndAlso Not line.StartsWith("//") Then
              Dim q As New Question
              Dim a() As String = line.Split("|"c)
              Dim b() As String = a(1).Split("#"c)
              Dim c() As String = b(0).Split(","c)
              With q
                .ID = i
                .Quest = a(0)
                .Choices = c.ToList
                .Answer = GetInteger(b(1))
              End With
              Questions.Add(q)
              i += 1
            End If
          Loop Until fn.EndOfStream
        End Using
      End Sub
      Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load
    
        GetQuestionsFromFile()
    
        ' set up controls
        Size = New Size(500, 400)
        With QtextBox
          .Font = New Font("Arial", 14)
          .Width = ClientSize.Width - 30
          .Location = New Point(20, 20)
          .Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
        End With
        With Panel
          .Location = New Point(20, QtextBox.Bottom + 20)
          .Width = ClientSize.Width - 130
          .Height = ClientSize.Height - 130
          .BorderStyle = BorderStyle.FixedSingle
          .Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Bottom
        End With
        With butt
          .Text = "Next Question"
          .AutoSize = True
          .Location = New Point(20, Panel.Bottom + 20)
          .Anchor = AnchorStyles.Left Or AnchorStyles.Bottom
        End With
        With RightWrong
          .Text = "Click button for Next Question"
          .Location = New Point(butt.Right + 28, butt.Top)
          .Font = New Font("Arial", 14)
          .AutoSize = True
        End With
        With Lab1
          .Text = "Right"
          .Location = New Point(Panel.Right + 18, Panel.Top)
          .Anchor = AnchorStyles.Right Or AnchorStyles.Top
        End With
        With lab2
          .Text = "Wrong"
          .Location = New Point(Panel.Right + 18, Panel.Top + 100)
          .Font = New Font("Arial", 14)
          .Anchor = AnchorStyles.Right Or AnchorStyles.Top
        End With
        With lab3
          .Text = "0"
          .Location = New Point(Panel.Right + 20, Panel.Top + 30)
          .Font = New Font("Arial", 24)
          .AutoSize = True
          .Anchor = AnchorStyles.Right Or AnchorStyles.Top
        End With
        With lab4
          .Text = "0"
          .Location = New Point(Panel.Right + 20, Panel.Top + 130)
          .Font = New Font("Arial", 24)
          .AutoSize = True
          .Anchor = AnchorStyles.Right Or AnchorStyles.Top
        End With
        Controls.AddRange({QtextBox, Panel, butt, RightWrong, Lab1, lab2, lab3, lab4})
        AddHandler butt.Click, AddressOf NewQuest
      End Sub
      Private Sub NewQuest(sender As Object, e As EventArgs)
        Static FirstQuestion As Boolean = True
        Static indx As Integer = 0
        If butt.Text = "New Game" Then
          RightWrong.Text = "Click button for Next Question"
          butt.Text = "Next Question"
          lab3.Text = "0"
          lab4.Text = "0"
          UsedID.Clear()
          Exit Sub
        End If
        If Not FirstQuestion Then
          Dim User As New UserInput With {.Quest = QtextBox.Text, .Answer = FindUserInput()}
          UserAnswers.Add(User)
          If User.Answer = Questions(indx).Answer Then
            lab3.Text = (GetInteger(lab3.Text) + 1).ToString
          Else
            lab4.Text = (GetInteger(lab4.Text) + 1).ToString
          End If
          If UsedID.Count >= Questions.Count Then
            RightWrong.Text = "Game Over with a Score of " & lab3.Text & " out of " & Questions.Count.ToString
            butt.Text = "New Game"
            Exit Sub
          End If
        End If
        Do
          indx = Rand.Next(0, Questions.Count)
        Loop While UsedID.Contains(indx)
        UsedID.Add(indx)
        QtextBox.Text = Questions(indx).Quest
        Dim y As Integer = 4
        Dim tg As Integer = 1
        Panel.Controls.Clear()
        For Each s As String In Questions(indx).Choices
          Dim rb As New RadioButton With {.Text = s, .Location = New Point(8, y), .Tag = tg}
          Panel.Controls.Add(rb)
          y += 20
          tg += 1
        Next
        FirstQuestion = False
      End Sub
      Private Function FindUserInput() As Integer
        Dim answer As Integer = -1
        For Each c As Control In Me.Panel.Controls
          If c.GetType() Is GetType(RadioButton) Then
            Dim rb As RadioButton = DirectCast(c, RadioButton)
            If rb.Checked Then
              answer = CInt(rb.Tag)
            End If
          End If
        Next
        Return answer
      End Function
      Private Function GetInteger(s As String) As Integer
        Dim v As Integer = 0
        If Integer.TryParse(s, v) Then Return v
        Return 0
      End Function
      Class Question
        Property ID As Integer
        Property Quest As String
        Property Choices As List(Of String)
        Property Answer As Integer
      End Class
      Class UserInput
        Property Quest As String
        Property Answer As Integer
      End Class
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by wingers Thursday, June 28, 2018 4:54 PM
    Wednesday, June 27, 2018 5:41 PM
  • @Les, I take it your not a cat person lol.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, June 27, 2018 6:39 PM
    Moderator
  • I would use an XML file to define the quiz so that you can write one code solution that can handle both types of questions for any size quiz.

    Here is a simple example.  In the real project you might encrypt the quiz file or save it as an embedded resource.  You could also add a class to keep track of results rather than just showing a correct/incorrect response.  But this should get you started and be easy to expand upon.

    <?xml version="1.0" encoding="utf-8" ?>
    <Quiz>
      <Question type="TrueFalse" answer="True">
        A whale is the largest marine mammal.
      </Question>
      <Question type="Multiple" answer="B">
        What is the largest marine mammal?
        <Choice letter="A" value="Elephant" />
        <Choice letter="B" value="Whale" />
        <Choice letter="C" value="Shark" />
        <Choice letter="D" value="Loch Ness Monster" />
      </Question>
      <Question type="Multiple" answer="E">
        Which of these are part of a computer?
        <Choice letter="A" value="CPU" />
        <Choice letter="B" value="RAM" />
        <Choice letter="C" value="Hard Drive" />
        <Choice letter="D" value="Motherboard" />
        <Choice letter="E" value="All of the above" />
      </Question>
    </Quiz>

    Code:

    Public Class Form1
        Private QuestionPanel As New Panel With {.Dock = DockStyle.Fill}
        Private WithEvents NextButton As New Button With {.Dock = DockStyle.Bottom, .Text = "Next Question"}
        Private YesNoPanel As New FlowLayoutPanel With {.Dock = DockStyle.Fill, .Name = "YesNoPanel"}
        Private MultiPanel As New FlowLayoutPanel With {.Dock = DockStyle.Fill, .Name = "MultiPanel"}
        Private YesNoQuestion As New Label With {.AutoSize = True}
        Private MultiQuestion As New Label With {.AutoSize = True}
        Private YesButton As New RadioButton With {.AutoSize = True, .Text = "True"}
        Private NoButton As New RadioButton With {.AutoSize = True, .Text = "False"}
        Private ChoiceA As New RadioButton With {.AutoSize = True, .Name = "ChoiceA"}
        Private ChoiceB As New RadioButton With {.AutoSize = True, .Name = "ChoiceB"}
        Private ChoiceC As New RadioButton With {.AutoSize = True, .Name = "ChoiceC"}
        Private ChoiceD As New RadioButton With {.AutoSize = True, .Name = "ChoiceD"}
        Private ChoiceE As New RadioButton With {.AutoSize = True, .Name = "ChoiceE"}
        Private ChoiceF As New RadioButton With {.AutoSize = True, .Name = "ChoiceF"}
    
        Private QuizDoc As XDocument
        Private QuestionNumber As Integer
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With YesNoPanel
                .Controls.Add(YesNoQuestion)
                .SetFlowBreak(YesNoQuestion, True)
                .Controls.Add(YesButton)
                .SetFlowBreak(YesButton, True)
                .Controls.Add(NoButton)
            End With
            With MultiPanel
                .Controls.Add(MultiQuestion)
                .SetFlowBreak(MultiQuestion, True)
                .Controls.Add(ChoiceA)
                .SetFlowBreak(ChoiceA, True)
                .Controls.Add(ChoiceB)
                .SetFlowBreak(ChoiceB, True)
                .Controls.Add(ChoiceC)
                .SetFlowBreak(ChoiceC, True)
                .Controls.Add(ChoiceD)
                .SetFlowBreak(ChoiceD, True)
                .Controls.Add(ChoiceE)
                .SetFlowBreak(ChoiceE, True)
                .Controls.Add(ChoiceF)
            End With
            Controls.Add(NextButton)
            Controls.Add(QuestionPanel)
    
            QuizDoc = XDocument.Load("Quiz1.xml")
            ShowNextQuestion()
        End Sub
    
        Private Sub LoadYesNoQuestion(question As XElement)
            If Not QuestionPanel.Controls.ContainsKey("YesNoPanel") Then
                QuestionPanel.Controls.Clear()
                QuestionPanel.Controls.Add(YesNoPanel)
            End If
            YesNoQuestion.Text = question.Value
            YesButton.Checked = False
            NoButton.Checked = False
            If question.@answer = "True" Then
                YesButton.Tag = True
                NoButton.Tag = False
            Else
                YesButton.Tag = False
                NoButton.Tag = True
            End If
        End Sub
    
        Private Sub LoadMultiQuestion(question As XElement)
            If Not QuestionPanel.Controls.ContainsKey("MultiPanel") Then
                QuestionPanel.Controls.Clear()
                QuestionPanel.Controls.Add(MultiPanel)
            End If
            MultiQuestion.Text = question.Value
            For Each ctrl In MultiPanel.Controls.OfType(Of RadioButton)
                ctrl.Checked = False
                ctrl.Visible = False
            Next
            For Each choice In question.<Choice>
                Dim ctrl = MultiPanel.Controls.Item("Choice" & choice.@letter)
                ctrl.Visible = True
                ctrl.Text = choice.@value
                If choice.@letter = question.@answer Then
                    ctrl.Tag = True
                Else
                    ctrl.Tag = False
                End If
            Next
        End Sub
    
        Private Sub ShowNextQuestion()
            If QuestionNumber < QuizDoc.Root.<Question>.Count Then
                Dim question = QuizDoc.Root.<Question>.ElementAt(QuestionNumber)
                If question.@type = "TrueFalse" Then
                    LoadYesNoQuestion(question)
                Else
                    LoadMultiQuestion(question)
                End If
                QuestionNumber += 1
            Else
                MessageBox.Show("You have reached the end of the quiz.")
            End If
        End Sub
    
        Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click
            Dim checkedAnswer = (From ctrl In QuestionPanel.Controls(0).Controls.OfType(Of RadioButton) Where ctrl.Checked = True).First
            If CBool(checkedAnswer.Tag) = True Then
                MessageBox.Show("Correct!")
            Else
                MessageBox.Show("Sorry, that is not correct.")
            End If
            ShowNextQuestion()
        End Sub
    End Class
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by wingers Thursday, June 28, 2018 4:54 PM
    Wednesday, June 27, 2018 6:45 PM
    Moderator
  • Bah, sorry, I didn't read this part close enough: "some would be select two answers from list below, some would be select all that apply from list below"

    However, it still should be some fairly minor changes to support that logic as well.  You could move the answer attribute off of the question and put it into the choice and then change the next button to get all choices with an answer attribute and see if they are all checked (using checkboxes instead of radio buttons).


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, June 27, 2018 6:57 PM
    Moderator
  • @Les, I take it your not a cat person lol.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Hi

    Ha ha, Actually I have had 2 cats for the last 13 years (sadly one has gone now), and I didn't throw any of them :)


    Regards Les, Livingston, Scotland

    Wednesday, June 27, 2018 7:22 PM
  • I believe Les and Reed are great options to peruse, just wanted to toss in a idea for working with a database.  No matter what direction you go consider having an active flag for a question as done in the Question table below and also consider a Boolean field for multiple choices or write in's.

     

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, June 27, 2018 8:00 PM
    Moderator
  • Thank you all very much for your replies, I will read properly tomorrow and give them all a try - can't concentrate tonight as got stinking headache

    :)


    Darren Rose

    Wednesday, June 27, 2018 8:36 PM
  • Thanks again for all your replies, with a mix of methods from all suggested I think I can now achieve what I wanted


    Darren Rose

    Thursday, June 28, 2018 4:54 PM