-1

我打算在 中制作一个入学考试窗口应用程序vb.net,VS2012,我想从数据库中随机选择问题(我使用 ms sql server 2005 express)而不是重复每个问题。我对使用绑定导航器的想法有限......是否可以使用绑定导航器随机选择,或者如果没有,您有任何想法、建议、教程或文章想要分享?这样做的最佳方法/解决方案是什么?

先感谢您!

4

1 回答 1

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
于 2013-09-18T03:43:58.733 回答