我一直致力于如何在Visual Basic中进行MCQ,并且在选择中至少有一个正确的答案。我被一个非常好的人建议应用一些修改然后修改它,但是我被困了,我需要帮助。
问题:当我点击button1有时我可以看到选择中没有正确的答案,这导致我再次点击button1跳过问题。我被建议好人将每个名字与答案联系起来,然后创建单独的表格,然后制作一些与答案相关的代码......我为我的理解和实现它的困难做好准备。 我的例子中的信息(Richard = IT,andy = commercial,simon = project,jim = English,chris = quality)
请帮助我一点,我真的很感激:
以下是代码:
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
非常感谢
答案 0 :(得分:0)
我很抱歉我现在没有时间做一个完整的解释,但是如果你在评论中告诉我我会在以后这样做。
您可以尝试这样的事情:
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
答案 1 :(得分:0)
您可以在检查radiobox时尝试启用boalean。 然后使用If语句检查
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
答案 2 :(得分:0)
我想我明白了。问题在于,因为您不需要模式,所以每个问题的选择都是从您对所有问题的总选择列表中随机选择的。通过这种方式,在许多情况下,您根本无法正确选择问题。
我认为应该有用,尝试将每个问题放在一个单独的表格上。这样可以防止不同问题的答案混淆。我不知道你是如何制作随机选择选择器的,但你不应该这样做。如果你这样做,你的错误对我来说似乎是不可避免的。你可以改变你的代码(我不知道如何,你应该问Mr.Nice Person),这样每次只能回答答案的顺序,而不是答案本身。
在尝试翻译代码之后,似乎我的错误理论是正确的。