另一种方法是使用 List(Of KeyValuePair(Of String, List(Of String)) 现在您可以按列表索引随机播放并将文本添加到与每个键关联的文本框中。
归根结底,您需要某种结构来保存每个人的值,并需要另一个结构来保存可以洗牌的人员集合。List(Of) 的工作非常适合这个。
正在为您制作一个示例,并意识到键值对可能比您需要的要多,所以我使用了一个更简单的 List(Of List(Of String)):
Public Class Form1
Dim rand As New Random(Now.Millisecond)
Dim ListOfValues As New List(Of List(Of String))
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'This routine will add 2 banks of textboxes to a form. Each bank will be _
8 rows and 3 columns. The left bank will use a naming pattern starting at _
'A' and the left bank starting at 'S'. I added the name of each one to the _
text so that you can make sure the naming pattern is being followed.
For I = 0 To 7
For j = 1 To 3
AddTB(I, j, "A"c)
AddTB(I, j, "S"c)
Next
Next
End Sub
Private Sub AddTB(row As Integer, column As Integer, start As Char)
Dim tb As New TextBox
Dim offset As Integer = Math.Sign(Asc(start) - 65) * (100 + tb.Width * 3)
tb.Name = "txt" & Chr(row + Asc(start)) & column.ToString
tb.Text = tb.Name
tb.Location = New Point(((column - 1) * tb.Width) + offset, (row * tb.Height))
Me.Controls.Add(tb)
End Sub
Private Function ShuffleInfo(ValuesToShuffle As List(Of List(Of String))) As List(Of List(Of String))
'this follows the same basic routine you were using, swapping each item with a random item.
For counter = 0 To ValuesToShuffle.Count - 1
Dim n = rand.Next(0, ValuesToShuffle.Count - 1)
Dim temp As List(Of String) = ValuesToShuffle(counter)
ValuesToShuffle(counter) = ValuesToShuffle(n)
ValuesToShuffle(n) = temp
Next
ShuffleInfo = ValuesToShuffle
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'this adds the data from the textboxes to the list. each row of data is a list
'inside the list. the controls collection can be indexed by control name.
'this makes it easy to access a specific control by using a naming pattern.
For I = 0 To 7
ListOfValues.Add({Me.Controls("txt" & Chr(I + 65) & "1").Text, _
Me.Controls("txt" & Chr(I + 65) & "2").Text, _
Me.Controls("txt" & Chr(I + 65) & "3").Text}.ToList)
Next
ListOfValues = ShuffleInfo(ListOfValues)
'This fills the other textboxes with the data from the shuffled list
For I = 0 To 7
Me.Controls("txt" & Chr(I + 83) & "1").Text = ListOfValues(I)(0)
Me.Controls("txt" & Chr(I + 83) & "2").Text = ListOfValues(I)(1)
Me.Controls("txt" & Chr(I + 83) & "3").Text = ListOfValues(I)(2)
Next
End Sub
End Class
这假定文本框的命名模式为行使用大写字母,列使用数字。(即“txtA3”是第一行中的第三个文本框。),对于输入文本框和相同的模式,但从'S' 用于打乱的数据。