我计划在vb.net,VS2012
制作入学考试窗口应用程序,我想从数据库中随机选择问题(我使用ms sql server 2005 express)而不是重复每一个问题。我对使用绑定导航器的想法有限......是否可以使用绑定导航器随机选择,或者如果没有,您是否有任何想要分享的想法,建议,教程或文章?这样做的最佳方式/解决方案是什么?
提前谢谢!
答案 0 :(得分:2)
试试这个:
Public Class Form1
Dim numberOfRecords As Integer
Dim questionArray() As Integer
Dim count As Integer = 0
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' Do logic here to load data from database
' Set the binding navigator's MoveNextItem property to nothing, because we are going to randomize the order instead of stepping through in order
BindingNavigator.MoveNextItem = Nothing
' Get the number of records here from binding source
numberOfRecords = BindingSource.Count
' Re-dimension array to size for total number of records
ReDim questionArray(numberOfRecords)
' Initialize array of questions
For i = 0 To numberOfRecords - 1
questionArray(i) = i + 1
Next
' Randomize the question array by moving items around
For i = 0 To numberOfRecords - 1
Dim swap As Integer = Int(((numberOfRecords - 1) * Rnd()) + 1)
Dim num As Integer = questionArray(i)
questionArray(i) = questionArray(swap)
questionArray(swap) = num
Next
End Sub
Private Sub BindingNavigatorMoveNextItem_Click(sender As System.Object, e As System.EventArgs) Handles BindingNavigatorMoveNextItem.Click
BindingSource.Position = questionArray(count)
' Set the stopping point
If count < numberOfRecords - 1 Then
count = count + 1
End If
End Sub
End Class