如何比较(排序)动态创建的文本框的值?

时间:2014-03-10 14:38:02

标签: vb.net

我目前正在做一个旨在对动态创建的文本框的值进行排序的程序。如何在vb.net中执行此操作?这是我创建文本框的代码:

For cnt = 0 To Val(TextBox1.Text) - 1
       arrivalbox.Add(New TextBox)
             With arrivalbox(cnt)
                .Parent = Me
                .Left = 0
                .Height = 13
                .Width = 65
                .Top = .Height * cnt + 50
                .Visible = True
                .Tag = cnt
                .Text = ""
                .Name = "arrival" & cnt
                .Location = New Point(380, 120 + (cnt * 25))
                .TextAlign = HorizontalAlignment.Center
                .Enabled = False
                Me.Controls.Add(New TextBox)
End With

1 个答案:

答案 0 :(得分:1)

这样的东西将遍历所有文本框并将它们添加到一个sortedlist中,该列表将根据值对所有文本框进行排序。

我不确定你在这里要做什么。但是,我猜测一旦你得到这些排序,那么你可以根据他们在排序列表中的位置来改变位置

Dim listedboxes As SortedList(Of Double, TextBox)
For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is TextBox Then
                  listedboxes.add(ctrl.value,ctrl)
            End if
Next

<强> 修改 也许是这样的。

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim listedboxes As New SortedList(Of Double, TextBox)
    For i As Integer = 0 To TextBox1.Text.Length - 1
        Dim tb As New TextBox
        With tb
            .Parent = Me
            .Left = 0
            .Height = 13
            .Width = 65
            .Top = .Height * i + 50
            .Visible = True
            .Tag = i
            .Text = TextBox1.Text.Substring(i, 1)
            .Name = "arrival" & i
            .TextAlign = HorizontalAlignment.Center
            .Enabled = False
            Me.Controls.Add(New TextBox)
        End With
        listedboxes.Add(TextBox1.Text.Substring(i, 1), tb)
    Next

    Dim j = 0
    For Each kvp As KeyValuePair(Of Double, TextBox) In listedboxes
        kvp.Value.Location = New Point(380, 120 + (j * 25))
        j += 1
    Next
End Sub

enter image description here