0

尝试通过文本框对象名称创建随机随机排序器。我有 8 行文本框,3 个文本框宽。例如:8 个不同人的姓名、电话号码、电子邮件地址。我想按顺序随机排序这些,但名称。电话号码和电子邮件地址必须保持一致,否则信息将是准确的。我可以使用以下代码仅针对名称文本框执行此操作,它可以完美运行。我如何让他其他两行文本框效仿。图形用户界面示例

文本框 文本框 文本框 按钮
文本框 文本框 文本框
文本框 文本框 文本框
文本框 文本框 文本框
文本框 文本框 文本框
文本框 文本框 文本框
文本框 文本框 文本框
文本框 文本框 文本框
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim ValuesToShuffle = New List(Of String)
    ValuesToShuffle.Add(txtA.Text)
    ValuesToShuffle.Add(txtB.Text)
    ValuesToShuffle.Add(txtC.Text)
    ValuesToShuffle.Add(txtD.Text)

    Dim rand = New Random()

    For counter = 0 To ValuesToShuffle.Count - 1
        Dim n = rand.Next(counter + 1)
        Dim temp = ValuesToShuffle(counter)
        ValuesToShuffle(counter) = ValuesToShuffle(n)
        ValuesToShuffle(n) = temp
    Next
    txtA.Text = ValuesToShuffle(0)
    txtB.Text = ValuesToShuffle(1)
    txtC.Text = ValuesToShuffle(2)
    txtD.Text = ValuesToShuffle(3)
4

2 回答 2

1

要么使用三个属性对结构或类进行混洗,要么使用三个文本框创建复合控件并对其进行混洗,使用 ValuesToShuffle 中的索引来计算 Y 坐标。

会有两种方式。

于 2013-09-10T13:45:38.400 回答
0

另一种方法是使用 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' 用于打乱的数据。

于 2013-09-10T13:59:13.507 回答