I am creating a multiple choice quiz but I am not sure how to check if the answer selected is the correct one.
I have

If radAnswer1.Checked = True Then

        End If

But do not know what to put after the "then". I am trying to compare the value in a label called lblAnswer1 with a value in my database called Correct Answer.
The value in lblAnswer1 is linked to the Answer 1 field in my database.
I will do the same for the other 3 possible answers.

Any help would be much appreciated.

Recommended Answers

All 6 Replies

The correct form of that expression is

If radAnswer1.Checked Then

You do not need to explicitly compare it to True because it already evaluates to a Boolean. I don't know enough to advise. What is the value of label? Is it the actual answer (as in several words) or is it something simple like A, B, C, etc.? If the latter than all you have to do is determine which radiobutton is checked and if it is the first one then compare label.Text to "A", and do likewise for the other choices. Keep in mind that there are likely better ways to display/test. Without knowing more about how you are doing it (you didn't post any code) I can't offer any suggestions.

Since you are using radio buttons, all you have to do is to determine which radio buttons are the correct answer and which one are not the correct answer so that when you check the correct answer the app will know that this is correct or not.

If radAnswer1.Checked Then 'Suppose this is the correct answer
MsgBox("Correct")
Else
MsgBox("Incorrect")
End If

For the following examples, assume you have a class variable named Score that keeps track of the current score. Also assume that you have the correct answer in a string variable, correctAnswer. If you name the buttons radA, radB, radC, etc. AND if you have the correct answer stored as A, B, C, etc. you can quickly check for a correct answer by

Dim rad As RadioButton = Me.Controls("rad" & correctAnswer)
If rad.Checked Then
    Score += 1
End If

A more concise form of that would be

If DirectCast(Me.Controls("rad" & correctAnswer), RadioButton).Checked Then
    Score += 1
End If

and even more concise

Score += IIf(DirectCast(Me.Controls("rad" & correctAnswer), RadioButton).Checked, 1, 0)

But what if the OP wants to have many questions, "GUI" based how will the OP sort an keep track of the questions and their core questions? Don't you think it will be a problem if the OP has maybe lets say 20 Questions and on each Question the OP has lets say 10, don't you think that will be a problem? not to mention that the OP here is using the radio buttons which only enables you to check one within the group or control so if the OP doesn't add any Control panel like Group Box to group the corresponding questions to gether he will have a problem in determining the right or wrong question on Question 1.2.

Meant If the OP doesn't add or use any Container not control panel.

Yes. If the idea is to present more than one multiple choice question at a time then a container like a GroupBox or Panel is required. Personally, I think it is better to present only one question at a time. What is gained by showing more than one?

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.