Hi, i have been working on a questionnaire that will allow me to show a question and then have three possible answers in the form of radio buttons. Users will have to select one then hit the NEXT button to move onto the next question. I have managed to use question case to do this and i can populate my form and the question and answers ok. My problem however, is that i need a way of adding up the scores so that every time the NEXT button is clicked the question is verified and then either adds 1 to a score or adds nothing. Finally this will show in a messagebox after all the questions are answered. I cant find anyhing on my problem and i have no idea what the code could be so i am very stuck. Any help is more than appreciated. The code for one of my questions is below and if any other code is needed please let me know. I have seen this done by my friend and he uses a 'CQUESTION' for the correct answer, although i do not know how he did this and the terms do not allow us to copy each other. Thanks!

lblQuestion.Text = ("How do you change the font style on all slides in a presentation that already has a theme applied?")
        rbAnswer1.Text = ("Change the fonts on the slide master")
        rbAnswer2.Text = ("After selecting all slides, click Fonts from the Home tab. Then select the new font style(s)")
        rbAnswer3.Text = ("Go to the Design tab and click Fonts. Then select the new font style(s)")
        If NQUESTION = 1 Then
            NQUESTION = NQUESTION + 1

Recommended Answers

All 5 Replies

You are going to need a counter to keep track of the number of correct answers. This will be a class level variable which will go after the "Public Class Form1" line (your actual class name may differ. The line will look like

Private numCorrect As Integer

You can set it to zero on form load (or anywhere else as long as it is set to zero before the quiz starts).

You should also create a sub to display the next question. I presume you either have an array of some data structure where each element contains

The question
The three choices
The correct choice

And you will have a button which will advance to the next question. This button can be disabled until the current question has been answered. The code to update numCorrect can go in the handler that is triggered when a radio button for an answer is selected.

The answer really depends on how you are storing the questions and answers. Are they in a database or simply hard coded into each screen?
If they are hardcoded you can include the answer as a hidden field (visible = false) and then just check it when the button is clicked. Note: hardcoding is a poor way to handle the problem.

Contents of QT.TXT:
Question|Answer1|Answer2|Answer3|NumberOfCorrectAnswer
Question|Answer1|Answer2|Answer3|NumberOfCorrectAnswer
...
Question|Answer1|Answer2|Answer3|NumberOfCorrectAnswer

i.e:
How do you change the font style on all slides in a presentation that already has a theme applied?|Change the fonts on the slide master|After selecting all slides, click Fonts from the Home tab. Then select the new font style(s)|Go to the Design tab and click Fonts. Then select the new font style(s)|1

(All in one line)

Use 4 RadioButtons
Rename RadioButton4 to RadioButton0 - set it to visible=false and checked=true

Imports System.IO

Public Class Form1
    Dim GivenAnswer As String
    Dim aktQNbr As Integer
    Dim TotalScore As Integer
    Dim CorrectAnswer As String
    Dim QuestionsArray() As String
    Dim LineArr() As String

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        GivenAnswer = "1"
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        GivenAnswer = "2"
    End Sub

    Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
        GivenAnswer = "3"
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LineArr = IO.File.ReadAllLines("C:\temp\QT.TXT")
        'Call LoadFile("C:\temp\QT.TXT")
        Call LoadQuestion(0)
    End Sub

    Sub LoadQuestion(ByVal QuestionNbr As Integer)
        GivenAnswer = "0"
        Dim QArr() As String
        Dim myLine As String
        aktQNbr = QuestionNbr
        myLine = LineArr(aktQNbr)
        QArr = Split(myLine, "|")
        txtQuestion.Text = QArr(0)
        RadioButton1.Text = QArr(1)
        RadioButton2.Text = QArr(2)
        RadioButton3.Text = QArr(3)
        CorrectAnswer = QArr(4)

    End Sub

    Sub LoadFile(ByVal xFile As String)
        LineArr = IO.File.ReadAllLines(xFile)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "EXIT" Then
            Me.Close()
        Else
            If GivenAnswer = CorrectAnswer Then TotalScore = TotalScore + 1
            If aktQNbr + 1 = LineArr.Length Then
                MessageBox.Show("Total Score = " & TotalScore.ToString, "Result", MessageBoxButtons.OK, MessageBoxIcon.Information)
                TotalScore = 0
                Button1.Text = "EXIT"
            Else
                LoadQuestion(aktQNbr + 1)
            End If
        End If

    End Sub
End Class

as what i have read.. your problem is that.. how to sum up scores each time the examinee gets the right answer...?
Try this if this could help:

Your starting form(lets assume questions starts here)

Public Class YourProject
Dim score As Integer
Dim choice1,choice2.choice3 As String

'Place event here to calculate the score
score=RightAnswer

'Pass the value to Form2 then show Form2
Form2.score=score
Me.close
Form2.Show
End Sub 
End Class

'Form2
Public Class Form2
Dim score As Integer
Dim choice1,choice2,choice3 As String
Dim myScore As Integer

'After declaring/Initializing variables WRITE THIS,
 Public WriteOnly Property score() As Integer
        Set(ByVal value As Integer)
            TheScore= value
        End Set
    End Property

'Create an event to Sum up Form1 score and Form2 score
score=MyScore + TheScore
'Cast result to Form3 score
Form3.score=score
Form3.Show
me.close
End Sub
End Class

'In Form3
Public Class Form3
Dim score As Integer
Dim choice1,choice2,choice3 As String
Dim myScore As Integer

'Do the same like in Form2..
.. 
...

Hope this helps.. if you have any question.. i'll try my best to answer it..

Sorry if i cant explain that in depth..but just cast the resulting score to the calling Forms score.. and do the same..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.