我正在创建一个多项选择测验,从访问数据库中获取问题和答案。
到目前为止,我已设法将数据库连接到表单并将问题输入到radiobuttons中。
我可以标记这个问题,但到目前为止我还没有弄清楚如何随机化数据库字段以进入不同的radiobuttons。我不希望在整个问题循环中,实际答案都在同一个单选按钮中。
附件是pic
形式的图片下面是我使用的代码。
button1是下一个按钮
button2是后退按钮
button3是保存按钮
button4是返回上一个表单按钮
button5是开始按钮
Imports System.Data.OleDb
Public Class Form1
Dim ConnectString As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
" Data Source= C:\Users\Sales\Documents\Visual Studio 2010\Normalised Database.accdb"
Dim dr As OleDbDataReader
Dim cm As New OleDbCommand
Dim cn As New OleDbConnection
Dim provider As String
Dim dataFile As String
Dim no As Integer
Dim q(no) As String
Dim quesnum As Integer
Dim answer As String
Dim selected As String
Dim score As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
no = InputBox("Input the number of questions you want. You can choose from 1 to 10.")
Label1.Hide()
Label2.Hide()
Label3.Hide()
RadioButton1.Hide()
RadioButton2.Hide()
RadioButton3.Hide()
RadioButton4.Hide()
Button1.Hide()
Button2.Hide()
Button2.Hide()
Button3.Hide()
Button4.Hide()
For i = 1 To no
question()
Next
End Sub
Private Sub question()
cn.ConnectionString = ConnectString
cn.Open()
quesnum = 1
Label1.Text = "Question " & quesnum & " of " & no
cm.CommandText = "SELECT Question, Answer1, Answer2, Answer3, CorrectAnswer FROM Question"
cm.Connection = cn
dr = cm.ExecuteReader
If dr.HasRows Then
dr.Read()
Label2.Text = dr.Item("Question")
RadioButton1.Text = dr.Item("Answer1")
RadioButton2.Text = dr.Item("Answer2")
RadioButton3.Text = dr.Item("Answer3")
RadioButton4.Text = dr.Item("CorrectAnswer")
dr.Close()
End If
cn.Close()
End Sub
Private Sub mark()
cn.ConnectionString = ConnectString
cn.Open()
cm.CommandText = "SELECT CorrectAnswer FROM Question"
cm.Connection = cn
dr = cm.ExecuteReader
If dr.HasRows Then
dr.Read()
answer = dr.Item("CorrectAnswer")
dr.Close()
End If
cn.Close()
If selected = answer Then score = score + 1
MsgBox("score is " & score)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If RadioButton1.Checked = True Then selected = RadioButton1.Text
mark()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Label1.Show()
Label2.Show()
Label3.Show()
RadioButton1.Show()
RadioButton2.Show()
RadioButton3.Show()
RadioButton4.Show()
Button1.Show()
Button2.Show()
Button3.Show()
Button4.Show()
Button5.Hide()
End Sub
End Class
我也无法循环查看问题,以便在按下下一个和后退按钮时显示不同的问题和答案。
非常感谢,谢谢!
答案 0 :(得分:2)
创建一个列表 - 例如
Dim answerList As New List(Of String)
并将您的查询结果添加到此 - 就像这样
answerList.Items.Add(dr.Item("Answer1"))
answerList.Items.Add(dr.Item("Answer2"))
answerList.Items.Add(dr.Item("Answer3"))
answerList.Items.Add(dr.Item("CorrectAnswer"))
然后这个函数随机化列表的顺序
Private Function RandomizeListOrder(answers As List(Of String)) As List(Of String)
Dim answer2 As Integer
Dim rnd As New Random
'loop though each item in the list
For answer1 As Integer = 0 To answers.Count - 1
'pick a random answer
answer2 = rnd.Next(0, answers.Count - 1)
'If answer1 and answer2 are the same then skip the rest of the code
'in the loop and go onto the next question
If answer1 = answer2 Then
Continue For
End If
'swap the questions over
Dim temp As String
temp = answers(answer1)
answers(answer1) = answers(answer2)
answers(answer2) = temp
Next
'return the list of answers
Return answers
End Function
喜欢这个
answerList = RandomizeListOrder(answerList)
然后将列表的每个元素添加到单选按钮
RadioButton1.Text = answerList(0)
RadioButton2.Text = answerlist(1)
RadioButton3.Text = answerList(2)
RadioButton4.Text = answerlist(3)
哦 - 听听Plutonix - 他是明智的。
<强>更新强>
希望当代码添加到您的代码中时,它应该看起来像这样 - 我想。我在我的电脑上测试了这个功能,似乎工作正常。如果仍然存在错误,则可能在您的代码中某处。
Imports System.Data.OleDb
Public Class Form1
Dim ConnectString As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
" Data Source= C:\Users\Sales\Documents\Visual Studio 2010\Normalised Database.accdb"
Dim dr As OleDbDataReader
Dim cm As New OleDbCommand
Dim cn As New OleDbConnection
Dim provider As String
Dim dataFile As String
Dim no As Integer
Dim q(no) As String
Dim quesnum As Integer
Dim answer As String
Dim selected As String
Dim score As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
no = InputBox("Input the number of questions you want. You can choose from 1 to 10.")
Label1.Hide()
Label2.Hide()
Label3.Hide()
RadioButton1.Hide()
RadioButton2.Hide()
RadioButton3.Hide()
RadioButton4.Hide()
Button1.Hide()
Button2.Hide()
Button2.Hide()
Button3.Hide()
Button4.Hide()
For i = 1 To no
question()
Next
End Sub
Private Sub question()
cn.ConnectionString = ConnectString
cn.Open()
quesnum = 1
Label1.Text = "Question " & quesnum & " of " & no
cm.CommandText = "SELECT Question, Answer1, Answer2, Answer3, CorrectAnswer FROM Question"
cm.Connection = cn
dr = cm.ExecuteReader
Dim answerList As New List(Of String)
If dr.HasRows Then
dr.Read()
Label2.Text = dr.Item("Question")
answerList.Items.Add(dr.Item("Answer1"))
answerList.Items.Add(dr.Item("Answer2"))
answerList.Items.Add(dr.Item("Answer3"))
answerList.Items.Add(dr.Item("CorrectAnswer"))
answerList = RandomizeListOrder(answerList)
RadioButton1.Text = answerList(0)
RadioButton2.Text = answerlist(1)
RadioButton3.Text = answerList(2)
RadioButton4.Text = answerlist(3)
dr.Close()
End If
cn.Close()
End Sub
Private Sub mark()
cn.ConnectionString = ConnectString
cn.Open()
cm.CommandText = "SELECT CorrectAnswer FROM Question"
cm.Connection = cn
dr = cm.ExecuteReader
If dr.HasRows Then
dr.Read()
answer = dr.Item("CorrectAnswer")
dr.Close()
End If
cn.Close()
If selected = answer Then score = score + 1
MsgBox("score is " & score)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If RadioButton1.Checked = True Then selected = RadioButton1.Text
mark()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Label1.Show()
Label2.Show()
Label3.Show()
RadioButton1.Show()
RadioButton2.Show()
RadioButton3.Show()
RadioButton4.Show()
Button1.Show()
Button2.Show()
Button3.Show()
Button4.Show()
Button5.Hide()
End Sub
Private Function RandomizeListOrder(answers As List(Of String)) As List(Of String)
Dim answer2 As Integer
Dim rnd As New Random
'loop though each item in the list
For answer1 As Integer = 0 To answers.Count - 1
'pick a random answer
answer2 = rnd.Next(0, answers.Count - 1)
'If answer1 and answer2 are the same then skip the rest of the code
'in the loop and go onto the next question
If answer1 = answer2 Then
Continue For
End If
'swap the questions over
Dim temp As String
temp = answers(answer1)
answers(answer1) = answers(answer2)
answers(answer2) = temp
Next
'return the list of answers
Return answers
End Function
End Class