vv
在完成关于简单数组排序的学校作业后,我想出了这个问题。假设我有一个带有名称的文本框。在旁边,我有一个带有数字的文本框。经验txtBox1 = "John Doe"
,txtBox2 = 8
。假设我有 10 行。那将是 20 个文本框。我怎么能按名称随机排序这些,将所有类似的数字按顺序排列在一起。输出应该看起来像这样。这里的关键是对同一号码组内的姓名进行随机排序。
- 约翰·多伊 3
- 玛丽珍 3
- 名称 4
- 名称 4
- 名称 4
- 名称 5
- 名称 7
- 名称 7
- 名称 8
- 名称 8
这是我拥有的代码。略有不同的是它有 3 列文本框和 8 行。这会随机对 3 行进行排序,将信息放在同一行中。John Doe,3 岁,电话号码。然后将信息放在文本框的镜像中。这个数字代表一个技能水平,所以我需要相同的技能水平才能发挥相同的技能水平,但在技能水平内随机排序。这是没有的。我不能有 3 打 7。我希望这能从那时起。就好像我需要一个顺序中的随机顺序。
Dim ListOfValues As New List(Of List(Of String))
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.txtA1.Focus()
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 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.
Button1.Enabled = True
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
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.
Dim rand As New Random(Now.Millisecond)
For counter = 0 To ValuesToShuffle.Count - 1
Dim n = rand.Next(counter + 1)
Dim temp As List(Of String) = ValuesToShuffle(counter)
ValuesToShuffle(counter) = ValuesToShuffle(n)
ValuesToShuffle(n) = temp
Next
ShuffleInfo = ValuesToShuffle
Button1.Enabled = False
End Function