I'v been working on how to do MCQ in visual basic with at least one correct answer among the choices. i'v been advised from a very nice person to apply some changes and amend it however, I'm stuck and I need assistance.
the issue: when I click the button1 sometimes I can see there is no correct answer among the choice which leads me to click button1 again to skip the question. i'v been advised from the nice person to connect each name with the answers and create separate table then make some codes to connected with the answers...I apolgise for my understanding and the difficulties doing it. for information in my example (Richard= IT , andy=commercial , simon= project , jim= English , chris= quality)
Please assist me for one point I really appreciated :
Below is the code:
Public Class Form1
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CheckBox1.Visible = True
CheckBox2.Visible = True
CheckBox3.Visible = True
CheckBox4.Visible = True
Dim wlistq As String() = {"richard", "chris", "andy", "simon", "jim"}
Label1.Text = wlistq(New Random().Next(0, wlistq.Length - 1))
Dim wlista As String() = {"IT", "quality", "commercial", "project", "english"}
Dim ra As New Random
For i = 1 To 4
Me.Controls("CheckBox" & i).Text = GetValue(wlista, ra, wlistq.Length - i)
Next
End Sub
Private Function GetValue(myList As String(), ra As Random, n As Integer) As String
Dim i As Integer
Dim tempStrng As String
i = ra.Next(0, n - 1)
tempStrng = myList(i)
myList(i) = myList(n)
Return tempStrng
End Function
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
CheckAnswer(sender)
End Sub
Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
CheckAnswer(sender)
End Sub
Private Sub CheckBox3_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox3.CheckedChanged
CheckAnswer(sender)
End Sub
Private Sub CheckBox4_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox4.CheckedChanged
CheckAnswer(sender)
End Sub
Private Function CheckAnswer(chkBox As CheckBox) As Boolean
If (Label1.Text = "richard" And chkBox.Text = "IT") Or (Label1.Text = "andy" And chkBox.Text = "commercial") Or (Label1.Text = "chris" And chkBox.Text = "quality") Then DisableCheckBoxes(chkBox)
End Function
Private Sub DisableCheckBoxes(chkBox As CheckBox)
For i = 1 To 4
If Me.Controls("CheckBox" & i).Name <> chkBox.Name Then Me.Controls("CheckBox" & i).Visible = False
Next
End Sub
End Class
thank you very much
I apologise that I don't have time right now for a full explanation, but if you ping me in the comments I'll do so later.
You could try something like this:
Public Class Form1
Private _checkboxes As CheckBox() = {CheckBox1, CheckBox2, CheckBox3, CheckBox4, CheckBox5}
Private _question As Question = New Question With
{
.Answer = "richard",
.Choices = New Dictionary(Of String, String) From
{
{"richard", "IT"},
{"chris", "quality"},
{"andy", "commercial"},
{"simon", "project"},
{"jim", "english"}
}
}
Private _answer As Integer
Private _shuffle As Integer()
Private _random As New Random()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
_shuffle = _
Enumerable _
.Range(0, _question.Choices.Count) _
.OrderBy(Function (x) _random.Next()) _
.ToArray()
_answer = _random.Next(0, _question.Choices.Count)
Label1.Text = _question.Choices.ElementAt(_answer).Key
For i = 0 To _question.Choices.Count
_checkboxes(i).Visible = True
_checkboxes(i).Text = _question.Choices.ElementAt(i).Value
Next
End Sub
Private Sub DisableCheckBoxes(chkBox As CheckBox)
For i = 1 To 4
If _checkboxes(i).Name <> chkBox.Name Then _checkboxes(i).Visible = False
Next
End Sub
End Class
Public Class Question
Public Choices As Dictionary(Of String, String)
Public Answer As String
End Class
You can try to enable a boalean when a radiobox is checked. Then use an If Statement to check
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If boolean1 = True Then .... 'write your code for what happens if it is true. Else .... 'If another one was checked instead.
End If End Sub
I think I figured it out. The problem is that because you don't want a pattern, your choices for each question are randomly selected from a list of total choices you have for all questions. In this way, in many cases you will not get a correct choice for the question at all.
What I think should work, try placing each question on a separate form.This will prevent answers of different questions to get mixed up. I don't know how you have you made your random choice selector,but you shouldn't do that. If you do, your error seems unavoidable to me. You can alter your code, (which I don't how, you should ask Mr.Nice Person) so that only the order of the answers is shuffled each time, not the answers themselves.
After a few attempts of translating your code, It seems my theory about your error is correct.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.